function SplitString(const Source:String;ASplit:string):fsArray;
var
AStr:String;
rArray:fsArray;
i:Integer;
begin
if Source='' then exit;
AStr:=Source;
i:=pos(ASplit,Source);
setlength(rArray,0);
while i<>0 do
begin
setlength(rArray,length(rArray) + 1);
rArray[length(rArray) - 1] := copy(AStr,0,i-1);
Delete(AStr,1,i);
i:=pos(ASplit,AStr);
end;
setlength(rArray,length(rArray) + 1);
rArray[length(rArray) - 1] := AStr;
Result := rArray;
end;
var
AStr:String;
rArray:fsArray;
i:Integer;
begin
if Source='' then exit;
AStr:=Source;
i:=pos(ASplit,Source);
setlength(rArray,0);
while i<>0 do
begin
setlength(rArray,length(rArray) + 1);
rArray[length(rArray) - 1] := copy(AStr,0,i-1);
Delete(AStr,1,i);
i:=pos(ASplit,AStr);
end;
setlength(rArray,length(rArray) + 1);
rArray[length(rArray) - 1] := AStr;
Result := rArray;
end;
fsArray = array of string;
var
tmpstr1, tmpstr2, tmpstr3: string;
begin
tmpstr1 := lowercase(s);
tmpstr2 := lowercase(startstr);
tmpstr3 := lowercase(endstr);
if pos(tmpstr2, tmpstr1) > 0 then
begin
delete(tmpstr1, 1, pos(tmpstr2, tmpstr1) - 1 + length(tmpstr2));
if (pos(tmpstr3, tmpstr1) > 0) then
begin
tmpstr1 := copy(tmpstr1, 1, pos(tmpstr3, tmpstr1) - 1);
result := tmpstr1;
end; end
else
result := '';end;用的时候
while pos('|',str)>0 do
begin
stringlist1.add(getstr(str,'|','|'));
delete(str,1,pos('|',str)-1+length('|'));
end;
这样才好
不用那个getstr函数就行
while pos('|',str)>0 do
begin
stringlist1.add(copy(str,1,pos('|',str)-1));
delete(str,1,pos('|',str)-1+length('|'));
end;//str为你的字符串
切割前,StringReplace 将空格都变成“#12345#”,反正就是每人会用的那种标示就可以了。
切割后,在每次取值时,再变回来。
逻辑非常简单。
Unit ClassesSyntax
[Delphi] function ExtractStrings(Separators: TSysCharSet; WhiteSpace: TSysCharSet; Content: PAnsiChar; Strings: TStrings): Integer;
Description Use ExtractStrings to fill a string list with the substrings of the null-terminated string specified by Content.
Separators is a set of characters that are used as delimiters, separating the substrings. Carriage returns, newline characters, and quote characters (single or double) are always treated as separators. Separators are ignored when inside a quoted string until the final end quote. (Note that quoted characters can appear in a quoted string if the quote character is doubled.)
WhiteSpace is a set of characters to be ignored when parsing Content if they occur at the beginning of a string.
Content is the null-terminated string to parse into substrings.
Strings is a string list to which all substrings parsed from Content are added. The string list is not cleared by ExtractStrings, so any strings already in the string list are preserved. ExtractStrings returns the number of strings added to the Strings parameter. Note:
ExtractStrings does not add empty strings to the list.
[update]:
Separators 参数指定一组分割符,所有的子串都是用它们分割的。但是成对的引号内的分割符会被忽略(参看下面的例子)。
WhiteSpace 参数指定每个子串开头被忽略的字符s。
Content 参数就是被分割的“源”串了。
Strings 参数用于接收分割后的各个子串。它的原有内容不会被清空。别忘了Create哦。
另外,EctractStrings不会把(忽略WhiteSpaces后的)空串加入到Strings中。写个例子吧:
比如
ABC|... DEF|#### GHI|"不会被分开|# www.farproc.com"
要得到
ABC
DEF
GHI
不会被分开|# www.farproc.com
四个子串可以用下面的代码:uses
Classes;
var
ASource: PChar;
AStr: String;
ACount: Integer;
AStrings: TStringList;
begin
ASource := 'ABC|... DEF|#### GHI|"不会被分开|# www.farproc.com"';
AStrings := TStringList.Create;
try
ACount := ExtractStrings(['|'], [' ', '#', '.'], ASource, AStrings);
{do any further processing}
/for AStr in AStrings do
// Writeln(AStr);
finally
AStrings.Free;
end; Readln;
end.
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=721257
var
ss:TStringDynArray;
procedure GetSplit(offset,count:integer);
var
index:integer;
begin
count:=count+1;
index:=PosEx(delimiter,S,offset);
if index>0 then
begin
GetSplit(index+1,count);
ss[count-1]:=copy(s,offset,index-offset);
end
else
begin
setlength(ss,count);
ss[count-1]:=copy(s,offset,length(s)-offset+1);
end;
end;
begin
GetSplit(1,0);
result:=ss;
end;
Split0
delphi中也有这样的函数(D7开始)
procedure Split(aValue : string; aDelimiter : Char; var Result : TStrings);
var
X : Integer;
S : string;
begin
if Result = nil then Result := TStringList.Create;
Result.Clear;
S := '';
for X:=1 to Length(aValue) do begin
if aValue[X] <> aDelimiter then
S:=S + aValue[X]
else begin
Result.Add(S);
S := '';
end;
end;
if S <> '' then Result.Add(S);
end;这个来自faststrings,效率绝对高,没有用到低效率的copy函数。
var
temp: WideString;
i: integer;
begin
result := tstringlist.Create;
temp := source;
i := pos(ch, temp);
while i <> 0 do
begin
result.Add(copy(temp, 1, i - 1));
delete(temp, 1, i);
i := pos(ch, temp);
end;
result.Add(temp);
end;
简单该第一个函数就可以了,注意copy的第二个参数,delphi中是从1开始的,lz写错了
另外用WideString就可以避免这个问题,其实就是“珅”字的第二个字节的值刚好是根‘|’一样
一句话就解决了
var
str:widestring;
i,len:integer;
begin
str:='';
for i:=0 to position-1 do
begin
if (pos(mask,s)<=0) then
begin
str:=s;
break;
end;
str:=copy(s,1,pos(mask,s)-1);
len:=length(str);
s:=copy(s,len+2,length(s)-len-1);
end;
result:=str;
end;procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i:=1 to 20 do
begin
memo1.Lines.Add(GetMaskString('王珅|身份证|320403323434581|市青年路71#-4-701|中国 |23400 |15952264449 85858342||||||','|',i))
end;
end;
memo1.Text:=stringreplace('王珅|身份证|320403323434581|市青年路71#-4-701|中国 |23400 |15952264449 85858342||||||','|',#13#10,[rfReplaceAll])
用stringreplace最简单!