﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-nighTuner &amp; Yuyu's Space-文章分类-Delphi</title><link>http://www.blogjava.net/nighTuner/category/1023.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 05:00:49 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 05:00:49 GMT</pubDate><ttl>60</ttl><item><title>dbgrid sort</title><link>http://www.blogjava.net/nighTuner/articles/3253.html</link><dc:creator>nighTuner</dc:creator><author>nighTuner</author><pubDate>Wed, 13 Apr 2005 17:56:00 GMT</pubDate><guid>http://www.blogjava.net/nighTuner/articles/3253.html</guid><wfw:comment>http://www.blogjava.net/nighTuner/comments/3253.html</wfw:comment><comments>http://www.blogjava.net/nighTuner/articles/3253.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nighTuner/comments/commentRss/3253.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nighTuner/services/trackbacks/3253.html</trackback:ping><description><![CDATA[<P>procedure GridTitleSort(Column: TColumn);<BR>Type<BR>&nbsp; TFieldTypeSet = set of TFieldType;<BR>var<BR>&nbsp; s, cFieldName: string;<BR>&nbsp; i: integer;<BR>&nbsp; DataSet: TDataSet;<BR>&nbsp; GridFieldTypeSet: TFieldTypeSet;<BR>&nbsp; procedure SetTitle;<BR>&nbsp; var<BR>&nbsp;&nbsp;&nbsp; ii: integer;<BR>&nbsp;&nbsp;&nbsp; cStr: string;<BR>&nbsp;&nbsp;&nbsp; c: TColumn;<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; for ii := 0 to TDBGrid(Column.Grid).Columns.Count - 1 do<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c := TDBGrid(Column.Grid).Columns[ii];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cStr := c.Title.Caption;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (pos('??', cStr) = 1) or (pos('??', cStr) = 1) then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Delete(cStr, 1, 2);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c.Title.Caption := cStr;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp; end;<BR>&nbsp; end;<BR>begin<BR>&nbsp; SetTitle;<BR>&nbsp; DataSet := Column.Grid.DataSource.DataSet;</P>
<P>&nbsp; GridFieldTypeSet := [ftString, ftSmallint, ftInteger, ftWord,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ftTime, ftDateTime, ftBytes, ftVarBytes,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ftTypedBinary, ftFixedChar, ftWideString,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ftLargeint, ftVariant];<BR>&nbsp; if not (Column.Field.DataType in GridFieldTypeSet) then Exit;&nbsp;&nbsp; //§P?_|r?q?t??</P>
<P>&nbsp; if Column.Field.FieldKind = fkLookup then<BR>&nbsp;&nbsp;&nbsp; cFieldName := Column.Field.KeyFields<BR>&nbsp; else if Column.Field.FieldKind = fkCalculated then<BR>&nbsp;&nbsp;&nbsp; cFieldName := Column.Field.KeyFields<BR>&nbsp; else<BR>&nbsp;&nbsp;&nbsp; cFieldName := Column.FieldName;<BR>&nbsp; //=================================AdoDataSet=====================</P>
<P>&nbsp; if DataSet is TCustomADODataSet then<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; s := TCustomADODataSet(DataSet).Sort;<BR>&nbsp;&nbsp;&nbsp; if s = '' then<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s := cFieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Column.Title.Caption := '??' + Column.Title.Caption;<BR>&nbsp;&nbsp;&nbsp; end<BR>&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if Pos(cFieldName, s) &lt;&gt; 0 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i := Pos('DESC', s);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if i &lt;= 0 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s := s + ' DESC';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Column.Title.Caption := '??' + Column.Title.Caption;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Column.Title.Caption := '??' + Column.Title.Caption;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Delete(s, i, 4);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s := cFieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Column.Title.Caption := '??' + Column.Title.Caption;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp; TCustomADODataSet(DataSet).Sort := s;<BR>&nbsp; end<BR>&nbsp;&nbsp;&nbsp; //============================Clientdataset==========================<BR>&nbsp; else if DataSet is TClientDataSet then<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; if TClientDataSet(DataSet).indexfieldnames &lt;&gt; '' then<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i := TClientDataSet(DataSet).IndexDefs.IndexOf('i' + Column.FieldName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if i = -1 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; with TClientDataSet(DataSet).IndexDefs.AddIndexDef do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name := 'i' + Column.FieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Fields := Column.FieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DescFields := Column.FieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TClientDataSet(DataSet).IndexFieldNames := '';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TClientDataSet(DataSet).IndexName := 'i' + Column.FieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Column.Title.Caption := '??' + Column.Title.Caption;<BR>&nbsp;&nbsp;&nbsp; end<BR>&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TClientDataSet(DataSet).IndexName := '';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TClientDataSet(DataSet).IndexFieldNames := column.fieldname;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Column.Title.Caption := '??' + Column.Title.Caption;<BR>&nbsp;&nbsp;&nbsp; end;<BR>&nbsp; end;<BR>end;<BR>//======================================================================</P>
<P><BR>procedure WWGridTitleSort(Grid: TwwDBGrid; TitleName: String);<BR>Type<BR>&nbsp; TFieldTypeSet = set of TFieldType;<BR>var<BR>&nbsp; s, cFieldName: string;<BR>&nbsp; i: integer;<BR>&nbsp; DataSet: TDataSet;<BR>&nbsp; aField: TField;<BR>&nbsp; GridFieldTypeSet: TFieldTypeSet;<BR>&nbsp; procedure SetTitle;<BR>&nbsp; var<BR>&nbsp;&nbsp;&nbsp; ii: integer;<BR>&nbsp;&nbsp;&nbsp; cStr: string;<BR>&nbsp;&nbsp;&nbsp; c: TwwColumn;<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; for ii := 0 to Grid.DataSource.DataSet.FieldCount -1 do<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c := Grid.Columns[ii];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cStr := c.DisplayLabel;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (pos('??', cStr) = 1) or (pos('??', cStr) = 1) then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Delete(cStr, 1, 2);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; c.DisplayLabel := cStr;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp; end;<BR>&nbsp; end;<BR>begin<BR>&nbsp; SetTitle;<BR>&nbsp; DataSet := Grid.DataSource.DataSet;<BR>&nbsp; aField := DataSet.FieldByName(TitleName);</P>
<P>&nbsp; GridFieldTypeSet := [ftString, ftSmallint, ftInteger, ftWord,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ftBoolean, ftFloat, ftCurrency, ftBCD, ftDate,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ftTime, ftDateTime, ftBytes, ftVarBytes,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ftTypedBinary, ftFixedChar, ftWideString,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ftLargeint, ftVariant];<BR>&nbsp; if not (aField.DataType in GridFieldTypeSet) then Exit;&nbsp;&nbsp; //§P?_|r?q?t??</P>
<P>&nbsp; if aField.FieldKind = fkLookup then<BR>&nbsp;&nbsp;&nbsp; cFieldName := aField.KeyFields<BR>&nbsp; else if aField.FieldKind = fkCalculated then<BR>&nbsp;&nbsp;&nbsp; cFieldName := aField.KeyFields<BR>&nbsp; else<BR>&nbsp;&nbsp;&nbsp; cFieldName := aField.FieldName;<BR>&nbsp; //=================================AdoDataSet=====================</P>
<P>&nbsp; if DataSet is TCustomADODataSet then<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; s := TCustomADODataSet(DataSet).Sort;<BR>&nbsp;&nbsp;&nbsp; if s = '' then<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s := cFieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aField.DisplayLabel := '??' + aField.DisplayLabel;<BR>&nbsp;&nbsp;&nbsp; end<BR>&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if Pos(cFieldName, s) &lt;&gt; 0 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i := Pos('DESC', s);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if i &lt;= 0 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s := s + ' DESC';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aField.DisplayLabel := '??' + aField.DisplayLabel;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aField.DisplayLabel := '??' + aField.DisplayLabel;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Delete(s, i, 4);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s := cFieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aField.DisplayLabel := '??' + aField.DisplayLabel;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp; TCustomADODataSet(DataSet).Sort := s;<BR>&nbsp; end<BR>&nbsp;&nbsp;&nbsp; //============================Clientdataset==========================<BR>&nbsp; else if DataSet is TClientDataSet then<BR>&nbsp; begin<BR>&nbsp;&nbsp;&nbsp; if TClientDataSet(DataSet).indexfieldnames &lt;&gt; '' then<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i := TClientDataSet(DataSet).IndexDefs.IndexOf('i' + aField.FieldName);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if i = -1 then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; with TClientDataSet(DataSet).IndexDefs.AddIndexDef do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name := 'i' + aField.FieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Fields := aField.FieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DescFields := aField.FieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TClientDataSet(DataSet).IndexFieldNames := '';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TClientDataSet(DataSet).IndexName := 'i' + aField.FieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aField.DisplayLabel := '??' + aField.DisplayLabel;<BR>&nbsp;&nbsp;&nbsp; end<BR>&nbsp;&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TClientDataSet(DataSet).IndexName := '';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TClientDataSet(DataSet).IndexFieldNames := aField.FieldName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aField.DisplayLabel := '??' + aField.DisplayLabel;<BR>&nbsp;&nbsp;&nbsp; end;<BR>&nbsp; end;<BR>end;<BR>//======================================================================</P><img src ="http://www.blogjava.net/nighTuner/aggbug/3253.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nighTuner/" target="_blank">nighTuner</a> 2005-04-14 01:56 <a href="http://www.blogjava.net/nighTuner/articles/3253.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Unsigned 64 bit Integer support</title><link>http://www.blogjava.net/nighTuner/articles/3252.html</link><dc:creator>nighTuner</dc:creator><author>nighTuner</author><pubDate>Wed, 13 Apr 2005 17:55:00 GMT</pubDate><guid>http://www.blogjava.net/nighTuner/articles/3252.html</guid><wfw:comment>http://www.blogjava.net/nighTuner/comments/3252.html</wfw:comment><comments>http://www.blogjava.net/nighTuner/articles/3252.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nighTuner/comments/commentRss/3252.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nighTuner/services/trackbacks/3252.html</trackback:ping><description><![CDATA[<P>unit UInt64Lib; <BR>{ <BR>&nbsp; Unsigned 64 bit Integer support for Delphi 6 and Kylix. <BR>&nbsp; Copyright (c) 2002 by DelphiFactory Netherlands BV </P>
<P>&nbsp; This unit provides to routines to convert between strings and unsigned <BR>&nbsp; 64 bit integers. </P>
<P>&nbsp; UInt64 range: 0..18446744073709551615 </P>
<P>&nbsp; Thanks to J.H.Plantenberg for the assembler part. </P>
<P>&nbsp; For comments mail to: <BR>&nbsp; <A href="mailto:erwin@delphi-factory.com">erwin@delphi-factory.com</A> <BR>} </P>
<P>interface </P>
<P>resourcestring <BR>&nbsp; SInvalidUInt64 = '''%s'' is not a valid UInt64 value'; </P>
<P>{ The UInt64 type is declared as a strong typed Int64, since both compilers <BR>&nbsp; don't support unsigned 64 bit integers. This way you can at least do some <BR>&nbsp; calculations. } <BR>type <BR>&nbsp; UInt64 = type Int64; </P>
<P>{ StrToUInt64 converts the given string to an unsigned 64 bit integer value. <BR>&nbsp; If the string doesn't contain a valid value, an EConvertError exception is <BR>&nbsp; raised. } <BR>function StrToUInt64(const S: AnsiString): UInt64; </P>
<P>{ UInt64ToStr converts the given value to its decimal string representation. } <BR>function UInt64ToStr(Value: UInt64): string; </P>
<P>implementation </P>
<P>// For speed we are going to use the fact that long strings are <BR>// guaranteed to be null-terminated: <BR>{$R-} // Range checking must be disabled <BR>{$B-} // Do not perform complete boolean expression evaluations </P>
<P>uses <BR>&nbsp; SysUtils; </P>
<P>type <BR>&nbsp; _UInt64 = packed record&nbsp;&nbsp; // Split the 64 bit into 2x32 bit <BR>&nbsp;&nbsp;&nbsp; Lo, Hi : LongWord; <BR>&nbsp; end; </P>
<P>procedure RaiseEConvertError(const S: AnsiString); <BR>begin <BR>&nbsp; // raise an exception explaining the input string was invalid <BR>&nbsp; raise EConvertError.CreateResFmt(@SInvalidUInt64, [S]); <BR>end; </P>
<P>function StrToUInt64(const S: AnsiString): UInt64; <BR>var <BR>&nbsp; I: LongWord; <BR>&nbsp; Dig: Integer; <BR>&nbsp; Digit : LongWord; <BR>&nbsp; Hi,Lo : LongWord; </P>
<P>begin <BR>&nbsp; // check if S is empty (is nil pointer) <BR>&nbsp; if S = '' then <BR>&nbsp;&nbsp;&nbsp; RaiseEConvertError(S); </P>
<P>&nbsp; // start at the first character <BR>&nbsp; I := 1; </P>
<P>&nbsp; // trim leading spaces <BR>&nbsp; while S[I] = ' ' do <BR>&nbsp;&nbsp;&nbsp; Inc(I); </P>
<P>&nbsp; if S[I] = '-' then // check for minus sign <BR>&nbsp;&nbsp;&nbsp; RaiseEConvertError(S); </P>
<P>&nbsp; if S[I] = '+' then // check for plus sign <BR>&nbsp;&nbsp;&nbsp; Inc(I); </P>
<P>&nbsp; // Check for hexidecimal string id:&nbsp; '$' or '0x' <BR>&nbsp; if (S[I] = '$') or ((S[I] = '0') and (Upcase(S[I+1]) = 'X')) then <BR>&nbsp; begin <BR>&nbsp;&nbsp;&nbsp; // trim leading zero (if '0x' hex marker) <BR>&nbsp;&nbsp;&nbsp; if S[I] = '0' then <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Inc(I); </P>
<P>&nbsp;&nbsp;&nbsp; // trim hex marker <BR>&nbsp;&nbsp;&nbsp; Inc(I); </P>
<P>&nbsp;&nbsp;&nbsp; // Check if empty <BR>&nbsp;&nbsp;&nbsp; if S[I] = #0 then <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RaiseEConvertError(S); </P>
<P>&nbsp;&nbsp;&nbsp; // init loop <BR>&nbsp;&nbsp;&nbsp; Dig := 0; <BR>&nbsp;&nbsp;&nbsp; Result := 0; </P>
<P>&nbsp;&nbsp;&nbsp; // while not end of string <BR>&nbsp;&nbsp;&nbsp; while S[I] &lt;&gt; #0 do <BR>&nbsp;&nbsp;&nbsp; begin <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // try character convert <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case S[I] of <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '0'..'9': Dig := Ord(S[I]) -&nbsp; Ord('0'); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'A'..'F': Dig := Ord(S[I]) - (Ord('A') - 10); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'a'..'f': Dig := Ord(S[I]) - (Ord('a') - 10); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RaiseEConvertError(S) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // still enough room in result? <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if Result shr 60 &gt; 0 then <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RaiseEConvertError(S); </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // shift converted digit into result <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result := Result shl 4 + Dig; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // next char <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Inc(I); <BR>&nbsp;&nbsp;&nbsp; end; <BR>&nbsp; end <BR>&nbsp; else begin&nbsp; // decimal unsigned 64 bit conversion </P>
<P>&nbsp;&nbsp;&nbsp; // check if not empty <BR>&nbsp;&nbsp;&nbsp; if S[I] = #0 then <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RaiseEConvertError(S); </P>
<P>&nbsp;&nbsp;&nbsp; Hi := 0; <BR>&nbsp;&nbsp;&nbsp; Lo := 0; <BR>&nbsp;&nbsp;&nbsp; while S[I] &lt;&gt; #0 do <BR>&nbsp;&nbsp;&nbsp; begin <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Extract the digit from the string and convert it ASCII-&gt;Byte <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Digit := Ord(S[I]) xor Ord('0'); </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Some assembler to perform an unsigned 64 bit integer calculation. <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // This asm code runs in D6 and Kylix (PIC code). <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp; HiLo := (HiLo*10)+Digit <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; asm <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;&nbsp; esi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // save register </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // calculate: Hi * 10; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp; eax, Hi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Load Hi <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp; ecx, 10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // multiplier is 10 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mul&nbsp;&nbsp;&nbsp; ecx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // EDX:EAX := EAX*ECX <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; or&nbsp;&nbsp;&nbsp;&nbsp; edx, edx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Overflow? <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; jnz&nbsp;&nbsp;&nbsp; @TooBig&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // yes -&gt; bye bye </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // calculate: Lo * 10 <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp; esi, eax&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // save Hi value <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp; eax, Lo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // load Lo <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mul&nbsp;&nbsp;&nbsp; ecx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // EDX:EAX := EAX*ECX </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Combine Hi, Lo, and overflow of Lo to form HiLo result <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add&nbsp;&nbsp;&nbsp; edx, esi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // EDX:EAX := HiLo*10 </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // HiLo := HiLo + Digit <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add&nbsp;&nbsp;&nbsp; eax, Digit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // EDX:EAX := HiLo+Digit <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; adc&nbsp;&nbsp;&nbsp; edx, 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // check overflow <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; jc&nbsp;&nbsp;&nbsp;&nbsp; @TooBig&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // yes -&gt; bye bye </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // save HiLo <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp; Hi, edx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Hi := EDX <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp; Lo, eax&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Lo := EAX <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; jmp&nbsp;&nbsp;&nbsp; @TheEnd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // successfull finish <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @TooBig: <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp; Digit, 666&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // something went wrong: invalidate Digit <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @TheEnd: </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop&nbsp;&nbsp;&nbsp; esi&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // restore register <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Check if digit was legal and if the previous calculation was a success <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if not (Digit in [0..9]) then <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RaiseEConvertError(S); </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // proceed to the next digit <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Inc(I); <BR>&nbsp;&nbsp;&nbsp; end; <BR>&nbsp;&nbsp;&nbsp; // Return HiLo as an unsigned 64 bit integer <BR>&nbsp;&nbsp;&nbsp; _UInt64(Result).Lo := Lo; <BR>&nbsp;&nbsp;&nbsp; _UInt64(Result).Hi := Hi; <BR>&nbsp; end; <BR>end; </P>
<P><BR>function UInt64ToStr(Value: UInt64): string; <BR>const <BR>&nbsp; BiggestUInt64Str = '18446744073709551615'; <BR>&nbsp; MaxBCD = Length(BiggestUInt64Str); </P>
<P>type <BR>&nbsp; TBCD = array[0..MaxBCD-1] of Integer; { Index 0 is highest BCD digit} </P>
<P>&nbsp; procedure AddBCD(var BCD : TBCD; Pos,Value : Integer); <BR>&nbsp; begin <BR>&nbsp;&nbsp;&nbsp; Inc(BCD[Pos], Value); <BR>&nbsp;&nbsp;&nbsp; if BCD[Pos] &gt; 9 then <BR>&nbsp;&nbsp;&nbsp; begin <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dec(BCD[Pos],10); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddBCD(BCD,Pos-1, 1); <BR>&nbsp;&nbsp;&nbsp; end; <BR>&nbsp; end; </P>
<P>&nbsp; procedure IncBCD(var A : TBCD; const B : TBCD); <BR>&nbsp; var <BR>&nbsp;&nbsp;&nbsp; I : Integer; <BR>&nbsp; begin <BR>&nbsp;&nbsp;&nbsp; for I := MaxBCD-1 downto 0 do <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddBCD(A, I, B[I]); <BR>&nbsp; end; </P>
<P>var <BR>&nbsp; ValueBCD : TBCD; <BR>&nbsp; BitValue : TBCD; <BR>&nbsp; Tmp : TBCD; <BR>&nbsp; I : Integer; <BR>&nbsp; Ofs : Integer; </P>
<P>begin <BR>&nbsp; // default to zero <BR>&nbsp; FillChar(ValueBCD, SizeOf(ValueBCD), 0); </P>
<P>&nbsp; // set bit value BCD. Lowest bit has value 1 <BR>&nbsp; FillChar(BitValue, SizeOf(BitValue), 0); <BR>&nbsp; BitValue[MaxBCD-1] := 1; </P>
<P>&nbsp; // check if there are bits available <BR>&nbsp; while Value &lt;&gt; 0 do <BR>&nbsp; begin <BR>&nbsp;&nbsp;&nbsp; // if current lowest bit is set <BR>&nbsp;&nbsp;&nbsp; //&nbsp; Increment the BCD value with the current bit value <BR>&nbsp;&nbsp;&nbsp; if Value and 1 &lt;&gt; 0 then <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IncBCD(ValueBCD, BitValue); </P>
<P>&nbsp;&nbsp;&nbsp; // proceed to the next bit <BR>&nbsp;&nbsp;&nbsp; Value := Value shr 1; </P>
<P>&nbsp;&nbsp;&nbsp; // Double the BitValue <BR>&nbsp;&nbsp;&nbsp; Tmp := BitValue; <BR>&nbsp;&nbsp;&nbsp; IncBCD(BitValue, Tmp); <BR>&nbsp; end; </P>
<P>&nbsp; // Find highest non zero decimal <BR>&nbsp; Ofs := 0; <BR>&nbsp; while (Ofs &lt; MaxBCD) and (ValueBCD[Ofs] = 0) do <BR>&nbsp;&nbsp;&nbsp; Inc(Ofs); </P>
<P>&nbsp; // check if any non zero decimals present <BR>&nbsp; if Ofs &lt; MaxBCD then <BR>&nbsp; begin <BR>&nbsp;&nbsp;&nbsp; // convert BCD result to ASCII result <BR>&nbsp;&nbsp;&nbsp; SetLength(Result, MaxBCD-Ofs); <BR>&nbsp;&nbsp;&nbsp; I := Ofs; <BR>&nbsp;&nbsp;&nbsp; Repeat <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result[I-Ofs+1] := Char(ValueBCD[I]+Ord('0')); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Inc(I); <BR>&nbsp;&nbsp;&nbsp; until I &gt; MaxBCD-1; <BR>&nbsp; end <BR>&nbsp; else <BR>&nbsp;&nbsp;&nbsp; Result := '0';&nbsp; // nothing set -&gt; value is '0' <BR>end; </P>
<P>end.</P><img src ="http://www.blogjava.net/nighTuner/aggbug/3252.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nighTuner/" target="_blank">nighTuner</a> 2005-04-14 01:55 <a href="http://www.blogjava.net/nighTuner/articles/3252.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>How to Working with Strings</title><link>http://www.blogjava.net/nighTuner/articles/3251.html</link><dc:creator>nighTuner</dc:creator><author>nighTuner</author><pubDate>Wed, 13 Apr 2005 17:54:00 GMT</pubDate><guid>http://www.blogjava.net/nighTuner/articles/3251.html</guid><wfw:comment>http://www.blogjava.net/nighTuner/comments/3251.html</wfw:comment><comments>http://www.blogjava.net/nighTuner/articles/3251.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nighTuner/comments/commentRss/3251.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nighTuner/services/trackbacks/3251.html</trackback:ping><description><![CDATA[<P>This article will help those that are trying to manipulate strings.&nbsp; These examples are simple, but give you an idea of what can be accomplished.</P>
<P>First off, let's say that we have a string that we need to replace all the spaces into something.&nbsp; We could use this:</P>
<P>StringReplace(Edit1.Text, ' ', '', [rfReplaceAll]);</P>
<P>This would go through Edit1.Text and replace all occurences of the space with nothing.&nbsp; It has only been since Delphi 4 that we have received this function, but below is the code to use in earlier versions.</P>
<P>function StringReplace(const S, OldPattern, NewPattern: string;<BR>&nbsp;Flags: TReplaceFlags): string;<BR>var<BR>&nbsp;SearchStr, Patt, NewStr: string;<BR>&nbsp;Offset: Integer;<BR>begin<BR>&nbsp;if rfIgnoreCase in Flags then<BR>&nbsp;begin<BR>&nbsp;&nbsp; SearchStr := AnsiUpperCase(S);<BR>&nbsp;&nbsp; Patt := AnsiUpperCase(OldPattern);<BR>&nbsp;end else<BR>&nbsp;begin<BR>&nbsp;&nbsp; SearchStr := S;<BR>&nbsp;&nbsp; Patt := OldPattern;<BR>&nbsp;end;<BR>&nbsp;NewStr := S;<BR>&nbsp;Result := '';<BR>&nbsp;while SearchStr &lt;&gt; '' do<BR>&nbsp;begin<BR>&nbsp;&nbsp; Offset := AnsiPos(Patt, SearchStr);<BR>&nbsp;&nbsp; if Offset = 0 then<BR>&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp; Result := Result + NewStr;<BR>&nbsp;&nbsp;&nbsp;&nbsp; Break;<BR>&nbsp;&nbsp; end;<BR>&nbsp;&nbsp; Result := Result + Copy(NewStr, 1, Offset - 1) + NewPattern;<BR>&nbsp;&nbsp; NewStr := Copy(NewStr, Offset + Length(OldPattern), MaxInt);<BR>&nbsp;&nbsp; if not (rfReplaceAll in Flags) then<BR>&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;&nbsp; Result := Result + NewStr;<BR>&nbsp;&nbsp;&nbsp;&nbsp; Break;<BR>&nbsp;&nbsp; end;<BR>&nbsp;&nbsp; SearchStr := Copy(SearchStr, Offset + Length(Patt), MaxInt);<BR>&nbsp;end;<BR>end;</P>
<P>I have seen many people trying to get different parts of a string to use, like the end of an email address.</P>
<P>Let's take for example that we want to get the first part of <A href="mailto:support@delphipages.com">support@delphipages.com</A>.</P>
<P>First of all we want to use the Copy function to copy the desired area, and the Pos function to find out just when the ampersand appears in the line of text.</P>
<P>Edit2.text := Copy(Edit1.Text, 1, Pos(<A href="mailto:'@'">'@'</A>, Edit1.Text)-1);</P>
<P>If we wanted the last part of the email:</P>
<P>Edit2.text := Copy(Edit1.Text, Pos(<A href="mailto:'@'">'@'</A>, Edit1.Text)+1, Length(Edit1.Text)-Pos(<A href="mailto:'@'">'@'</A>, Edit1.Text));</P>
<P>These two problems are fairly simple, but what if you had a string like this:</P>
<P>10,10,100,45,T,support@delphipages.com,80,douglas,tietjen</P>
<P>I could run through a series of Copy and Pos and Delete functions to be able to get the email address, but it would sure look hectic and hard to figure out problems.</P>
<P>I also develop with Cold Fusion, and I found some useful commands, and so I recreated them for me in Delphi, and others have done similiar things.</P>
<P>First off I wrote a function called ListGetAt.</P>
<P>function ListGetAt(List: String; Position: Integer; Delimiter: String = ','): String;</P>
<P>function TForm1.ListGetAt(List: String; Position: Integer;<BR>&nbsp;Delimiter: String): String;<BR>var<BR>&nbsp; i, NP: integer;<BR>begin<BR>&nbsp; NP := 0;<BR>&nbsp; for i := 1 to Position do begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; List := Copy(List,NP,Length(List)-NP+1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NP := Pos(Delimiter,List)+Length(Delimiter);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if i = Position then begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if Pos(Delimiter, List) = 0 then break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Delete(List, NP-Length(Delimiter), Length(List)-(NP-Length(Delimiter)-1));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<BR>&nbsp; end;<BR>&nbsp; Result := List;<BR>end;</P>
<P>Now, anytime that I need to get a section of code I can call this function for quick and easy access.&nbsp; For example, now if I wanted the email address, I could write the following:</P>
<P>Edit2.Text := ListGetAt(Edit1.Text, 6, ',');</P>
<P>I would now have <A href="mailto:'support@delphipages.com'">'support@delphipages.com'</A>.</P>
<P>If I just wanted the last part of the email, I could type this.</P>
<P>Edit2.Text := ListGetAt(ListGetAt(Edit1.Text, 6, ','), 2, <A href="mailto:'@'">'@'</A>);</P>
<P>Now suppose that the list of items were values that you wanted to change and store for later use.&nbsp; It would be quite hectic again to go through and find each part and update it's value.&nbsp; So I created another function called ListSetAt.</P>
<P>function ListSetAt(List: String; Position: Integer; Value: string; Delimiter: String = ','): String;</P>
<P>function TForm1.ListSetAt(List: String; Position: Integer; Value,<BR>&nbsp;Delimiter: String): String;<BR>var<BR>&nbsp; i, NP: integer;<BR>&nbsp; BegStr, EndStr: string;<BR>begin<BR>&nbsp;NP := 0;<BR>&nbsp;BegStr := '';<BR>&nbsp;EndStr := '';<BR>&nbsp;for i := 1 to Position do begin<BR>&nbsp;&nbsp; if i &gt; 1 then<BR>&nbsp;&nbsp;&nbsp;&nbsp; BegStr := BegStr+Copy(List, 0, Pos(Delimiter,List)+Length(Delimiter)-1);<BR>&nbsp;&nbsp; List := Copy(List,NP,Length(List)-NP+Length(Delimiter));<BR>&nbsp;&nbsp; NP := Pos(Delimiter,List)+Length(Delimiter);<BR>&nbsp;&nbsp; if i = Position then begin<BR>&nbsp;&nbsp;&nbsp;&nbsp; if Pos(Delimiter, List) = 0 then break;<BR>&nbsp;&nbsp;&nbsp;&nbsp; EndStr := Copy(List, NP-Length(Delimiter), Length(List)-(NP-Length(Delimiter)-1));<BR>&nbsp;&nbsp; end;<BR>&nbsp;end;<BR>&nbsp;Result := BegStr+Value+EndStr;<BR>end;</P>
<P>Now if I was to update the email address, I could just type this.</P>
<P>Edit1.Text := ListSetAt(Edit1.Text, 6, <A href="mailto:'email@email.com'">'email@email.com'</A>, ',');</P>
<P>It would now update just the sixth position.&nbsp; Now you could use it all sorts of ways.&nbsp; If I just wanted everything after the ampersand, then I could type this</P>
<P>ListGetAt(Edit1.Text, 2, <A href="mailto:'@'">'@'</A>);</P>
<P>It would give the result of: delphipages.com,80,douglas,tietjen</P>
<P>These functions are simple, for example, if you were to ListGetAt on Position 0 it would return the entire line back.&nbsp; If you did it on Position 20, it would return the last item in the list.&nbsp; In Cold Fusion they have a command called ListLen, so you can find out how many ListItems you are working with.</P>
<P><BR>function ListLen(List: String; Delimiter: String=','): Integer;</P>
<P>function TForm1.ListLen(List: String; Delimiter: String): Integer;<BR>begin<BR>&nbsp; Result := 1;<BR>&nbsp; While Pos(Delimiter, List) &gt; 0 do begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Delete(List, 1, Pos(Delimiter, List)+Length(Delimiter)-1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Result := Result + 1;<BR>&nbsp; end;<BR>end;<BR></P><img src ="http://www.blogjava.net/nighTuner/aggbug/3251.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nighTuner/" target="_blank">nighTuner</a> 2005-04-14 01:54 <a href="http://www.blogjava.net/nighTuner/articles/3251.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>