delphi string.split 按照任意字符串分割语句

时间:2021-07-26 21:43:39

var
  I: Integer;
  S: string;
  A: TArray<string>;
begin
  S := 'AAA分割bbb分割ccc';
  A := S.Split(['分割']);
  for I := 0 to High(A) do
    ShowMessage(A[I]);
end;

 

输出结果为:

AAA

BBB

CCC

 

var

  str: string;
  arr: TArray<string>;
begin
  str := 'A-1,B-2,,,C-3,D-4';

  arr := str.Split([',']);                                   // arr[0] = A-1; Length(arr) = 6
  arr := str.Split([','], TStringSplitOptions.ExcludeEmpty); // 忽略空项; Length(arr) = 4
  arr := str.Split([','], 2);                                // 只提取前 2

  arr := str.Split([',', '-'], ExcludeEmpty); //arr[0] = A; Length(arr) = 8

  arr := str.Split([',,,'], None);            // 分隔符可以是一个字符串数组
end;