var i,j:integer; //j记录|上一次位置 begin j:=0; for i:=1 to length(S1) do begin if copy(S1,i,1)='|' then begin s2.Add(trim(copy(s1,j+1,i-j-1))); j:=i; end; end; end; 说明:S2是我定义的TStringList,它的每一项就是你想要得到的东西
var s:string; o:array[1..3] of string; begin s:='12|45|66'; o[1]:=copy(s,1,pos('|',s)-1); delete(s,1,pos('|',s)); o[2]:=copy(s,1,pos('|',s)-1); delete(s,1,pos('|',s)); o[3]:=s; end;上面只是一个简单的例子,说明一下字符串的操作函数的使用。运用这些字符串函数,再加上循环处理,可以非常方便的进行各种字符串操作。用Copy、Length、Pos、Delete四个函数基本可以处理所有字符串操作。建议了解以下字符串处理函数,灵活运用可能收到良好效果: AnsiLowerCase LowerCase AnsiCompareStr CompareStr AppendStr Pos Copy Delete Length Insert
用一个简单的方法。 var ts: TStringList; begin ts := TStringList.Create; ts.Text := StringReplace('ab|cd|ef|gh', '|', #13#10, [rfReplaceAll]); // 这时 ts.Strings 中就是一个数组了。 ts.Free; end;
var s,a,b:string; i,d,b1:integer; a1:array [1..32] of string; begin a:='12|45|66|77|7777777'; b:='|'; i:=1; s:=''; while i<=length(a) do begin if (copy(a,i,1)=b) and (s<>'') then begin s:=copy(a,d+1,i-d-1); a1[i]:=s; end; if (copy(a,i,1)=b) and (s='') then begin s:=copy(a,1,i-1); a1[i]:=s; end; if copy(a,i,1)=b then d:=i; if i=length(a) then begin s:=copy(a,d+1,i); a1[i]:=s; end;i:=i+1; end; end;字符型函数a1的值就是取出的字符串,如a1[3]的值就是12,表示字符串的第三个字符是特殊符号,取出的值就是'12'.
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.用我的这个单元吧,hehe^^
i,j:integer; //j记录|上一次位置
begin
j:=0;
for i:=1 to length(S1) do
begin
if copy(S1,i,1)='|' then
begin
s2.Add(trim(copy(s1,j+1,i-j-1)));
j:=i;
end;
end;
end;
说明:S2是我定义的TStringList,它的每一项就是你想要得到的东西
s:string;
o:array[1..3] of string;
begin
s:='12|45|66';
o[1]:=copy(s,1,pos('|',s)-1);
delete(s,1,pos('|',s));
o[2]:=copy(s,1,pos('|',s)-1);
delete(s,1,pos('|',s));
o[3]:=s;
end;上面只是一个简单的例子,说明一下字符串的操作函数的使用。运用这些字符串函数,再加上循环处理,可以非常方便的进行各种字符串操作。用Copy、Length、Pos、Delete四个函数基本可以处理所有字符串操作。建议了解以下字符串处理函数,灵活运用可能收到良好效果:
AnsiLowerCase
LowerCase
AnsiCompareStr
CompareStr
AppendStr
Pos
Copy
Delete
Length
Insert
var
ts: TStringList;
begin
ts := TStringList.Create;
ts.Text := StringReplace('ab|cd|ef|gh', '|', #13#10, [rfReplaceAll]);
// 这时 ts.Strings 中就是一个数组了。
ts.Free;
end;
i,d,b1:integer;
a1:array [1..32] of string;
begin
a:='12|45|66|77|7777777';
b:='|';
i:=1;
s:='';
while i<=length(a) do
begin
if (copy(a,i,1)=b) and (s<>'') then
begin
s:=copy(a,d+1,i-d-1);
a1[i]:=s;
end;
if (copy(a,i,1)=b) and (s='') then
begin
s:=copy(a,1,i-1);
a1[i]:=s;
end;
if copy(a,i,1)=b then d:=i;
if i=length(a) then
begin
s:=copy(a,d+1,i);
a1[i]:=s;
end;i:=i+1;
end;
end;字符型函数a1的值就是取出的字符串,如a1[3]的值就是12,表示字符串的第三个字符是特殊符号,取出的值就是'12'.
StringReplace('ab|cd|ef|gh', '|','##', [rfReplaceAll]) 的函数返回结果是 'ab##cd##ef##gh',还有一个选项是 [rfrfIgnoreCase] 表示忽略大小写。
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.用我的这个单元吧,hehe^^