楼上几位借助 TStrings 的方法如果字符串中包含回车换行就不能用了,要么先把回车换行替换处理,要么用另外的方法。我这里有一组专门处理这些字符串的函数供参考,其中字符串转成数组的有两个不同的,一个借助TStrings处理(对于包含回车换行的有问题),另一个直接处理字符串(效率低一点)<br><br>function StrExistsCount(S,Sub:string;CanRepeat:boolean;IsCaseSensitive:boolean):integer;<br>var i,j:integer;<br> s1:string;<br> len:integer;<br>begin //一个字符串在另一个字符串中出现的次数<br> Result:=0;<br> if length(s)<length(sub) then exit;<br> j:=0;<br> len:=length(sub);<br> if IsCaseSensitive then<br> s1:=sub<br> else<br> s1:=LowerCase(sub);<br><br> if CanRepeat then<br> begin<br> for i:=1 to length(s)-len+1 do<br> begin<br> if IsCaseSensitive then<br> begin<br> if copy(s,i,len)=s1 then inc(j)<br> end<br> else<br> begin<br> if LowerCase(copy(s,i,len))=s1 then inc(j)<br> end;<br> end;<br> end<br> else<br> begin<br> i:=1;<br> while i<=length(s)-len+1 do<br> begin<br> if IsCaseSensitive then<br> begin<br> if copy(s,i,len)=s1 then<br> begin<br> inc(j);<br> i:=i+len;<br> Continue;<br> end;<br> end<br> else<br> begin<br> if LowerCase(copy(s,i,len))=s1 then<br> begin<br> inc(j);<br> i:=i+len;<br> Continue;<br> end;<br> end;<br> inc(i); <br> end;<br> end;<br> Result:=j;<br>end;<br><br>function DimLBound(d:Variant):integer;<br>begin //数组下限<br> if VarIsArray(d) then<br> Result:=VarArrayLowBound(d,1)<br> else<br> Result:=-1;<br>end;<br><br>function DimHBound(d:Variant):integer;<br>begin //数祖上限<br> if VarIsArray(d) then<br> Result:=VarArrayHighBound(d,1)<br> else<br> Result:=-1; <br>end;<br><br><br>function IsDim(d:Variant):boolean;<br>begin //判断是否为数据变量<br> Result:=VarIsArray(d);<br>end;<br><br>function DimCount(d:Variant):integer;<br>begin //数组长度<br> if VarIsArray(d) then<br> Result:=DimHBound(d)-DimLBound(d)+1<br> else<br> Result:=-1;<br>end;<br><br>function DimToStr(d:Variant;Separator:string):string;<br>var i:integer;<br> s:string;<br>begin //数组合成字符串<br> s:='';<br> if VarIsArray(d) then<br> for i:=DimLBound(d) to DimHBound(d) do<br> s:=s+String(d)+Separator;<br> if s<>'' then delete(s,length(s)-length(Separator)+1,length(Separator));<br> Result:=s;<br>end;<br><br>function StrToDim(S,Separator:string):Variant;<br>var d:Variant;<br> i,l:integer;<br> StrList:TStrings;<br>begin //字符串转换为数(组若内容包含#13,则数据不正确)<br> Result:=Null;<br> if s='' then exit;<br> s:=StringReplace(s,Separator,#13#10,[rfReplaceAll,rfIgnoreCase]);<br> StrList:=TStringList.Create;<br> try<br> StrList.Text:=s;<br> l:=StrList.Count-1;<br> d:=VarArrayCreate([0,l],varVariant);<br> i:=0;<br> while i<=l do<br> begin<br> d:=StrList.Strings;<br> inc(i);<br> end;<br> finally<br> FreeAndNil(StrList);<br> end;<br> Result:=d;<br>end;<br><br>function StrToDim(S,Separator:string):Variant;<br>var d:Variant;<br> i,j,k,len,len1,l:integer;<br>begin //字符串转换为数组(效率不高,代码可读性差)<br> Result:=Null;<br> if s='' then exit;<br> s:=s+separator;<br> l:=StrExistsCount(s,separator,false,false);<br> separator:=LowerCase(separator);<br> len:=length(s);<br> len1:=length(Separator);<br> k:=0;<br> d:=VarArrayCreate([0,l-1],varVariant);<br> i:=1;<br> while i<=len-len1 do<br> begin<br> for j:=i to len do<br> if LowerCase(Copy(s,j,len1))=separator then<br> begin<br> if k<=l-1 then d[k]:=copy(s,i,j-i);<br> inc(k);<br> break;<br> end;<br> i:=j+len1;<br> end;<br> Result:=d;<br>end;<br><br><br>--------------------------------------<br>var v:Variant;<br> s:string;<br>begin<br> s:='12,23,12,234,12234,231'<br> v:=StrToDim(s,',');<br> v[0]:=……<br> v[1]:=……<br>end;