下面是我项目中自己写的,常用的取串涵数//从字符串S中,取出(从左到右)第Index个以Split为分隔符的子串,Index从1开始... Function SubOfIndex(S :string; Index :integer; Const Split :string=';'):string; var i,k,m,n:integer; str,substr:string; begin if trim(s)='' then exit; if Index<1 then exit; if Split='' then exit; str := S; i:= Pos(Split,str); //当只有一个子串时,是不出现分隔符的 if (i<1) then begin if (Index=1) then result := s; exit; end; m:= 0; repeat inc(m); if i<1 then begin substr := str; str := ''; end else begin substr := copy(str,1,i-1); str := copy(str,i+length(Split),length(str)); i := Pos(Split,str); end; until (m=Index) or (str=''); if m=Index then result := substr; end;........ 有了上面的涵数就好做了 str := '001+035+047+100+158...'; str1 := SubOfIndex(str, 1, '+'); //取以'+'分隔的第一个子串 str2 := SubOfIndex(str, 2, '+'); //取以'+'分隔的第一个子串 str3 := SubOfIndex(str, 3, '+'); //取以'+'分隔的第一个子串 str4 := SubOfIndex(str, 4, '+'); //取以'+'分隔的第一个子串 ... ... ... ...至于转为整型,存放到数组中,那就很容易,自己写吧
根据某个字符分割字符串的函数 procedure SeparateTerms(s : string;Separator : char;Terms : TStringList); { This browses a string and divide it into terms whenever the given separator is found. The separators will be removed } var hs : string; p : integer;begin Terms.Clear; // First remove all remaining terms if Length(s)=0 then // Nothin' to separate Exit; p:=Pos(Separator,s); while P<>0 do begin hs:=Copy(s,1,p-1); // Copy term Terms.Add(hs); // Add to list Delete(s,1,p); // Remove term and separator p:=Pos(Separator,s); // Search next separator end; if Length(s)>0 then Terms.Add(s); // Add remaining term end;这个函数把字符串分割成TStringList,然后在做相应的转换,变成整数和数组。
Function SubOfIndex(S :string; Index :integer; Const Split :string=';'):string;
var
i,k,m,n:integer;
str,substr:string;
begin
if trim(s)='' then exit;
if Index<1 then exit;
if Split='' then exit;
str := S;
i:= Pos(Split,str); //当只有一个子串时,是不出现分隔符的
if (i<1) then
begin
if (Index=1) then
result := s;
exit;
end; m:= 0;
repeat
inc(m);
if i<1 then
begin
substr := str;
str := '';
end
else
begin
substr := copy(str,1,i-1);
str := copy(str,i+length(Split),length(str));
i := Pos(Split,str);
end;
until (m=Index) or (str='');
if m=Index then
result := substr;
end;........
有了上面的涵数就好做了
str := '001+035+047+100+158...';
str1 := SubOfIndex(str, 1, '+'); //取以'+'分隔的第一个子串
str2 := SubOfIndex(str, 2, '+'); //取以'+'分隔的第一个子串
str3 := SubOfIndex(str, 3, '+'); //取以'+'分隔的第一个子串
str4 := SubOfIndex(str, 4, '+'); //取以'+'分隔的第一个子串
... ...
... ...至于转为整型,存放到数组中,那就很容易,自己写吧
str2 := SubOfIndex(str, 2, '+'); //取以'+'分隔的第2个子串
str3 := SubOfIndex(str, 3, '+'); //取以'+'分隔的第3个子串
str4 := SubOfIndex(str, 4, '+'); //取以'+'分隔的第4个子串
procedure SeparateTerms(s : string;Separator : char;Terms : TStringList);
{ This browses a string and divide it into terms whenever the given
separator is found. The separators will be removed }
var
hs : string;
p : integer;begin
Terms.Clear; // First remove all remaining terms
if Length(s)=0 then // Nothin' to separate
Exit;
p:=Pos(Separator,s);
while P<>0 do
begin
hs:=Copy(s,1,p-1); // Copy term
Terms.Add(hs); // Add to list
Delete(s,1,p); // Remove term and separator
p:=Pos(Separator,s); // Search next separator
end;
if Length(s)>0 then
Terms.Add(s); // Add remaining term
end;这个函数把字符串分割成TStringList,然后在做相应的转换,变成整数和数组。
s := '001+035+047+100+158...';
news := copy(s, 1, pos('+', s)); // news就是'001'
procedure SpliteStr;
var
str1: string;
SL: TStringlist;
begin
str1 := '001+035+047+100+158...';
SL := TStringlist.Create;
try
SL.text := StringReplace(str1, '+', #13,[rfReplaceAll, rfIgnoreCase]);
ShowMessage('a1=' + SL[0]);
ShowMessage('a2=' + SL[1]);
finally
end;
end
牛,果然是另有想法啊!
var
str1: string;
SL: TStringlist;
begin
str1 := '001+035+047+100+158...';
SL := TStringlist.Create;
try
sl.Delimiter := '+';
sl.DelimitedText := str1;
ShowMessage('a1=' + SL[0]);
ShowMessage('a2=' + SL[1]);
finally
SL.Free;
end;
end;
返回string字符串中的字符char的位置,比如:
pos(lifeixiao,x)返回结果为6.
pos('lifeixiao','x')
这么容易的方法都能想到
yaqiangsu那段代码好象是我写的,但没想到 huojiehai(海天子) 如果牛!佩服!佩服!
var
a:string;
b:tstringlist;
begin
a:='a+b+c';
b:=tstringlist.Create();
b.Delimiter:='+';
b.DelimitedText:=a;
showmessage(b.Strings[1]);end; //delphi 6