type TResultArray = array of string;function SplitString(const source, ch: string): TResultArray; var temp: string; i: integer; begin temp := source; i := pos(ch, source); while i <> 0 do begin SetLength(Result, Length(Result) + 1); Result[Length(Result) - 1] := copy(temp, 0, i - 1); delete(temp, 1, i); i := pos(ch, temp); end; SetLength(Result, Length(Result) + 1); Result[Length(Result)-1] := Temp; end; ************** function SplitString(const source,ch:string):tstringlist; var temp:string; i:integer; begin result:=tstringlist.Create; temp:=source; i:=pos(ch,source); while i<>0 do begin result.Add(copy(temp,0,i-1)); delete(temp,1,i); i:=pos(ch,temp); end; result.Add(temp); end; 调用: s:=splitstring('afsdfsdaaa|bbfdsfsdb|ccc','|'); for i:=0 to s.Count-1 do b:=b+s.Strings[i]+#13; showmessage(b); s.free;
放到一个TListBox中吧: var TmpStr,SrcStr:String; begin SrcStr:='111|BPS|222|BPS|333|BPS|444|BPS|555' ListBox1.Items.Clear; while Pos('|',SrcStr)>0 do begin TmpStr:=Copy(SrcStr,1,Pos('1',SrcStr)-1); Delete(StrStr,1,Pos('1',SrcStr)); if TmpStr<>'' then ListBox1.Items.Add(TmpStr); end; if SrcStr<>'' then ListBox1.Items.Add(StrStr); end; //随手写的,可能有些不对的地方,给你参照一下吧
分割字符串函数 Function TForm1.CutString(Str:String):String; var N:integer; Tmp:String; begin Tmp:=''; N:=pos('|BPS|',Str); While (N>0) do begin Tmp:=tmp+copy(Str,N-3,3)+','; str:=copy(str,N+5,length(str)); N:=pos('|BPS|',Str); end; Result:=Tmp+Str; end;//调用函数 procedure TForm1.Button1Click(Sender: TObject); var S:string; begin S:='111|BPS|222|BPS|333|BPS|444|BPS|555'; label1.Caption:=CutString(S); end;
我给你写一个通用性的。//s:分割符; str :字符串 function TForm1.MyPos(s, str: string): TstringList; var i,j: Integer; trmSList: TstringList; begin trmSList := TStringList.Create; trmSList.Add(''); j := 0; for i := 1 to Length(str) do if s = str[i] then begin trmSList.Add(''); j := j + 1; end else trmSList.Strings[j] := trmSList.Strings[j] + str[i]; Result := trmSList; end;procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(Self.MyPos(Edit1.Text,Edit2.Text).Text); end;
找到再说
TResultArray = array of string;function SplitString(const source, ch: string): TResultArray;
var
temp: string;
i: integer;
begin
temp := source;
i := pos(ch, source);
while i <> 0 do
begin
SetLength(Result, Length(Result) + 1);
Result[Length(Result) - 1] := copy(temp, 0, i - 1);
delete(temp, 1, i);
i := pos(ch, temp);
end;
SetLength(Result, Length(Result) + 1);
Result[Length(Result)-1] := Temp;
end;
**************
function SplitString(const source,ch:string):tstringlist;
var
temp:string;
i:integer;
begin
result:=tstringlist.Create;
temp:=source;
i:=pos(ch,source);
while i<>0 do
begin
result.Add(copy(temp,0,i-1));
delete(temp,1,i);
i:=pos(ch,temp);
end;
result.Add(temp);
end;
调用:
s:=splitstring('afsdfsdaaa|bbfdsfsdb|ccc','|');
for i:=0 to s.Count-1 do
b:=b+s.Strings[i]+#13;
showmessage(b);
s.free;
var
TmpStr,SrcStr:String;
begin
SrcStr:='111|BPS|222|BPS|333|BPS|444|BPS|555'
ListBox1.Items.Clear;
while Pos('|',SrcStr)>0 do
begin
TmpStr:=Copy(SrcStr,1,Pos('1',SrcStr)-1);
Delete(StrStr,1,Pos('1',SrcStr));
if TmpStr<>'' then ListBox1.Items.Add(TmpStr);
end;
if SrcStr<>'' then
ListBox1.Items.Add(StrStr);
end;
//随手写的,可能有些不对的地方,给你参照一下吧
Function TForm1.CutString(Str:String):String;
var
N:integer;
Tmp:String;
begin
Tmp:='';
N:=pos('|BPS|',Str);
While (N>0) do
begin
Tmp:=tmp+copy(Str,N-3,3)+',';
str:=copy(str,N+5,length(str));
N:=pos('|BPS|',Str);
end;
Result:=Tmp+Str;
end;//调用函数
procedure TForm1.Button1Click(Sender: TObject);
var
S:string;
begin
S:='111|BPS|222|BPS|333|BPS|444|BPS|555';
label1.Caption:=CutString(S);
end;
其实实现这种功能的算法不少,自己仔细琢磨琢磨。编一个很容易的!
function TForm1.MyPos(s, str: string): TstringList;
var
i,j: Integer;
trmSList: TstringList;
begin
trmSList := TStringList.Create;
trmSList.Add('');
j := 0;
for i := 1 to Length(str) do
if s = str[i] then
begin
trmSList.Add('');
j := j + 1;
end
else
trmSList.Strings[j] := trmSList.Strings[j] + str[i]; Result := trmSList;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(Self.MyPos(Edit1.Text,Edit2.Text).Text);
end;