Delphi

使用TStringList 的 DelimitedText ,如果被分割字符串中包含空格,被自动分割,这恐怕不是想要的结果,解决办法:用自定义的分割函数!

 1   function SplitString(const Source, ch: String): TStringList;
 2   var
 3     Temp: String;
 4     I: Integer;
 5     chLength: Integer;
 6   begin
 7     Result := TStringList.Create;
 8     // 如果是空自符串则返回空列表
 9     if Source = '' then
10       Exit;
11     Temp := Source;
12     I := Pos(ch, Source);
13     chLength := Length(ch);
14     while I <> 0 do
15     begin
16       Result.Add(Copy(Temp, 0, I - 1));
17       Delete(Temp, 1, I - 1 + chLength);
18       I := Pos(ch, Temp);
19     end;
20     Result.Add(Temp);
21   end;

示例:

tempList := SplitString('被分割的字符串', '分割符号');

参考:

https://www.cnblogs.com/coolsundy/p/3833684.html

原文地址:https://www.cnblogs.com/sunylat/p/14035106.html