procedure SeparateTerms(s : string;Separator : char;Terms : TStringList); var hs : string; p : integer;begin Terms.Clear; // 清除所有的terms if Length(s)=0 then // 如没有任何分隔字符存在就退出 Exit; p:=Pos(Separator,s); while P<>0 do begin hs:=Copy(s,1,p-1); // 复制 term Terms.Add(hs); // 加入 term Delete(s,1,p); // 清除分隔符 p:=Pos(Separator,s); // 寻找下一个分隔符 end; if Length(s)>0 then Terms.Add(s); // 结尾剩余字符加入 term end; //使用办法var Terms : TStringList;begin
同意楼上 应该用 If Pos('|',str1)<>0 Then Begin i:=Pos('|',str1); str_1:=Copy(str1,1,i-1); delete(str1,1,i); End; .........
有写好的东西,你搜索看看;其实是VB中的SPLITE
给你大概写一个,自己去改改应该没问题的。 j:=0; for i:=0 to len(str1) do begin k:=pos('|',str1); str[j]:=copy(str1,i,i-1); i:=pos('|',str1); str1:=copy(str1,i-1,len(str1); j:=j+1; end;
我以前写的一个函数 { sSource:要分割的字符串 sSplit:分隔符 lstData:分割后的字符串列表 返回:分割的字符串个数 } function MySplitString(const sSource, sSplit: String;var lstData: TStrings): Integer; var iIndex, iPreIndex: Integer; pStr: PChar; begin lstData.Clear; pStr := PChar(sSource); iPreIndex := 0; iIndex := Pos(sSplit, pStr); if iIndex = 0 then lstData.Add(sSource); while (iIndex > iPreIndex) and (iPreIndex <= Length(sSource))do begin lstData.Add(Copy(pStr + iPreIndex, 1, iIndex - iPreIndex - 1)); iPreIndex := iIndex + Length(sSplit) - 1; iIndex := Pos(sSplit, pStr + iPreIndex) + iPreIndex; if iIndex = iPreIndex then iIndex := Length(sSource) + 1; end; result := lstData.Count; end;
var
i:integer;
str_1,str_2,str_3:string;
i:=Pos('|',str1);
str_1:=Copy(str1,1,i-1);
delete(str1,1,i);
i:=Pos('|',str1);
str_2:=Copy(str1,1,i-1);
delete(str1,1,i); i:=Pos('|',str1);
str_2:=Copy(str1,1,i-1);
delete(str1,1,i);
在此只是做个示范,应该设计为循环实现
var
hs : string;
p : integer;begin
Terms.Clear; // 清除所有的terms
if Length(s)=0 then // 如没有任何分隔字符存在就退出
Exit;
p:=Pos(Separator,s);
while P<>0 do
begin
hs:=Copy(s,1,p-1); // 复制 term
Terms.Add(hs); // 加入 term
Delete(s,1,p); // 清除分隔符
p:=Pos(Separator,s); // 寻找下一个分隔符
end;
if Length(s)>0 then
Terms.Add(s); // 结尾剩余字符加入 term
end;
//使用办法var
Terms : TStringList;begin
Terms:=TStringList.Create;
str1='TEST|44.5|4'
SeparateTerms(str1,'|',Terms);
Terms.Free;
end;
应该用
If Pos('|',str1)<>0 Then
Begin
i:=Pos('|',str1);
str_1:=Copy(str1,1,i-1);
delete(str1,1,i);
End;
.........
j:=0;
for i:=0 to len(str1) do
begin
k:=pos('|',str1);
str[j]:=copy(str1,i,i-1);
i:=pos('|',str1);
str1:=copy(str1,i-1,len(str1);
j:=j+1;
end;
{
sSource:要分割的字符串
sSplit:分隔符
lstData:分割后的字符串列表
返回:分割的字符串个数
}
function MySplitString(const sSource, sSplit: String;var lstData: TStrings): Integer;
var
iIndex, iPreIndex: Integer;
pStr: PChar;
begin
lstData.Clear;
pStr := PChar(sSource);
iPreIndex := 0;
iIndex := Pos(sSplit, pStr);
if iIndex = 0 then
lstData.Add(sSource); while (iIndex > iPreIndex) and (iPreIndex <= Length(sSource))do
begin
lstData.Add(Copy(pStr + iPreIndex, 1, iIndex - iPreIndex - 1));
iPreIndex := iIndex + Length(sSplit) - 1;
iIndex := Pos(sSplit, pStr + iPreIndex) + iPreIndex;
if iIndex = iPreIndex then
iIndex := Length(sSource) + 1;
end;
result := lstData.Count;
end;
我借用一下 shuixin13(犬犬(心帆)) 的函数声明,写一写我经常用的这一段吧
procedure SeparateTerms(s : string;Separator : char;Terms : TStringList);
begin
Terms.Text :=StringReplace(s,Separator,#13,[rfReplaceAll]);
end;使用方法呢,跟shuixin13(犬犬(心帆)) 的一样哦