自己建一个函数,很容易实现的 string:Tstrings; .... while true do begin i:=pos('#',sStr); if i>0 then begin str1:=copy(sStr,1,i); Strings.add(str1); delete(sStr,1,i+1); end else break; end;
unit ToolsUnit;interface function StrLeft(const mStr: String; mDelimiter: String): String; function ListCount(mList: String; mDelimiter: String): Integer; function ListValue(mList: String; mIndex: Integer; mDelimiter: String =','): String; function PosEx(Substr: string; S: string): Integer;implementationuses Windows;//============================ StrLeft =====================================// function StrLeft(const mStr: string; mDelimiter: string): string; { 返回左分隔字符串 } begin Result := Copy(mStr, 1, Pos(mDelimiter, mStr) - 1); end;//=========================== ListCount =====================================// function ListCount(mList: string; mDelimiter: string ): Integer; { 返回列表数 } var I, L: Integer; mListC:String; begin Result := 0; if mList = '' then Exit; mListC := mList; L := Length(mList); I := Pos(mDelimiter , mList); while I > 0 do begin mList := Copy(mList , I + Length(mDelimiter), L); I := Pos(mDelimiter , mList); Inc(Result); end; Inc(Result); if PChar(mListC)[0] = mDelimiter then Result := Result - 1; if PChar(mListc)[Length(mListc)-1] = mDelimiter then Result := Result - 1; end;//============================= ListValue ==================================// function ListValue(mList: string; mIndex: Integer; mDelimiter: string = ','): string; { 返回列表指定位置的元素 } var I, L, K: Integer; begin if Pchar(mList)[Length(mList)-1] <> mDelimiter then mList:=mList + mDelimiter; L := Length(mList); I := Pos(mDelimiter, mList); K := 0; Result := ''; while (I > 0) and (K <> mIndex) do begin mList := Copy(mList, I + Length(mDelimiter), L); I := Pos(mDelimiter, mList); Inc(K); end; if K = mIndex then Result := StrLeft(mList + mDelimiter, mDelimiter); end;//=============================== PosEx =====================================// function PosEx(Substr: string; S: string): Integer; var CharMap: array [Char] of Char; I, J: Integer; Ps: Pchar; Mark: Boolean; C: Char; begin Result := 0; for C := Low(CharMap) to High(CharMap) do CharMap[C] := C; AnsiUpperBuff(PChar(@CharMap), sizeof(CharMap)); AnsiUpperBuff(@Substr[1], Length(Substr)); Ps := PChar(S); for I := 1 to Length(S) do begin if CharMap[(Ps + I - 1)^] = SubStr[1] then begin Mark := True; for J := 1 to Length(Substr) do begin if Charmap[(Ps + I + J - 2)^] <> SubStr[J] then begin Mark := False; Break; end; end; if Mark then begin Result := I; Break; end; end; end; end;end. 这是我写的一个常用的ToolUnit单元,你用用吧hehe^^
string:Tstrings;
....
while true do
begin
i:=pos('#',sStr);
if i>0 then
begin
str1:=copy(sStr,1,i);
Strings.add(str1);
delete(sStr,1,i+1);
end
else break;
end;
StrList.DelimitedText := '#125#148#3648#2214';
var
Count: Integer;
begin
StringList.Clear;
repeat
Count := Pos(Sign, S);
StringList.Add(Copy(S, 1, Count -1));
S := Copy(S, Count + 1, Length(S) - Count);
until StrPos(PChar(S), PChar(Sign)) = nil;
StringList.Add(S);
end;例如数据是 ‘125#148#3648#2214’
StringList: TStringList;
SeparateString(‘125#148#3648#2214’,'#',StringList);
StringList[0] := '125';
StringList[1] := '148';
StringList[2] := '3648';
StringList[3] := '2214';只写了个大概的
StrList.Delimiter := '#';
StrList.DelimitedText := '#125#148#3648#2214';
StrList[0]...StrList[n]中就是你想要的了
function ListCount(mList: String; mDelimiter: String): Integer;
function ListValue(mList: String; mIndex: Integer; mDelimiter: String =','): String;
function PosEx(Substr: string; S: string): Integer;implementationuses
Windows;//============================ StrLeft =====================================//
function StrLeft(const mStr: string; mDelimiter: string): string;
{ 返回左分隔字符串 }
begin
Result := Copy(mStr, 1, Pos(mDelimiter, mStr) - 1);
end;//=========================== ListCount =====================================//
function ListCount(mList: string; mDelimiter: string ): Integer;
{ 返回列表数 }
var
I, L: Integer;
mListC:String;
begin
Result := 0;
if mList = '' then Exit;
mListC := mList;
L := Length(mList);
I := Pos(mDelimiter , mList);
while I > 0 do begin
mList := Copy(mList , I + Length(mDelimiter), L);
I := Pos(mDelimiter , mList);
Inc(Result);
end;
Inc(Result);
if PChar(mListC)[0] = mDelimiter then
Result := Result - 1;
if PChar(mListc)[Length(mListc)-1] = mDelimiter then
Result := Result - 1;
end;//============================= ListValue ==================================//
function ListValue(mList: string; mIndex: Integer; mDelimiter: string = ','): string;
{ 返回列表指定位置的元素 }
var
I, L, K: Integer;
begin
if Pchar(mList)[Length(mList)-1] <> mDelimiter then
mList:=mList + mDelimiter;
L := Length(mList);
I := Pos(mDelimiter, mList);
K := 0;
Result := '';
while (I > 0) and (K <> mIndex) do begin
mList := Copy(mList, I + Length(mDelimiter), L);
I := Pos(mDelimiter, mList);
Inc(K);
end;
if K = mIndex then
Result := StrLeft(mList + mDelimiter, mDelimiter);
end;//=============================== PosEx =====================================//
function PosEx(Substr: string; S: string): Integer;
var
CharMap: array [Char] of Char;
I, J: Integer;
Ps: Pchar;
Mark: Boolean;
C: Char;
begin
Result := 0;
for C := Low(CharMap) to High(CharMap) do CharMap[C] := C;
AnsiUpperBuff(PChar(@CharMap), sizeof(CharMap));
AnsiUpperBuff(@Substr[1], Length(Substr));
Ps := PChar(S);
for I := 1 to Length(S) do
begin
if CharMap[(Ps + I - 1)^] = SubStr[1] then
begin
Mark := True;
for J := 1 to Length(Substr) do
begin
if Charmap[(Ps + I + J - 2)^] <> SubStr[J] then
begin
Mark := False;
Break;
end;
end;
if Mark then
begin
Result := I;
Break;
end;
end;
end;
end;end.
这是我写的一个常用的ToolUnit单元,你用用吧hehe^^