//分割字符串 ExtractStrings
var
s: String;
List: TStringList;
begin
s := 'about: #delphi; #pascal, programming';
List := TStringList.Create;
ExtractStrings([';',',',':'],['#',' '],PChar(s),List);
//第一个参数是分隔符; 第二个参数是开头被忽略的字符
ShowMessage(List.Text); //about
//delphi
//pascal
//programming
List.Free;
end;
修改源码中的一行,可以处理"5,,1,2,3,湖北省-荆州市-荆州荆中路"连续分隔符的情况。
if (Head <> Tail) and (Head^ <> #0) then改成 if (Head^ <> #0) then即可。
另外一个分隔字符串过程(由IdGlobal原来过程修改而成)
procedure SplitDelimitedString(const AData: string; AStrings: TStrings; const ADelim: string = ' ');
var
i: Integer;
LData: string;
LDelim: Integer; //delim len
LLeft: string;
LLastPos, LLeadingSpaceCnt: PtrInt;
begin
Assert(Assigned(AStrings));
AStrings.Clear;
LDelim := Length(ADelim);
LLastPos := 1;
LData := AData;
if LData = '' then begin //if WhiteStr
Exit;
end;
LLeadingSpaceCnt := 0;
while AData[LLeadingSpaceCnt + 1] <= #32 do begin
Inc(LLeadingSpaceCnt);
end;
i := Pos(ADelim, LData);
while I > 0 do begin
LLeft := Copy(LData, LLastPos, I - LLastPos); //'abc d' len:=i(=4)-1 {Do not Localize}
AStrings.Add(Trim(LLeft));
LLastPos := I + LDelim; //first char after Delim
i := PosIdx(ADelim, LData, LLastPos);
end;//while found
if LLastPos <= Length(LData) then begin
AStrings.Add(Trim(Copy(LData, LLastPos, MaxInt)));
end;
end;