当前位置 : 主页 > 编程语言 > delphi >

Delphi分割字符串函数Split源码

来源:互联网 收集:自由互联 发布时间:2021-06-23
function TStringHelper.Split( const Separator: array of string ; Count: Integer; Options: TStringSplitOptions): TArray string ; var P: Integer; Total: Integer; Index: Integer; S, ToSplit: string ; begin Total : = 0 ; ToSplit : = Self; P : =
function TStringHelper.Split(const Separator: array of string; Count: Integer;
  Options: TStringSplitOptions): TArray<string>;
var
  P: Integer;
  Total: Integer;
  Index: Integer;
  S, ToSplit: string;
begin
  Total := 0;
  ToSplit := Self;
  P := ToSplit.IndexOfAny(Separator, Index);
  while (P >= 0) and (Total < Count) do
  begin
    S := ToSplit.Substring(0, P);
    if (S <> Empty) or ((S = Empty) and (Options <> ExcludeEmpty)) then
    begin
      Inc(Total);
      SetLength(Result, Total);
      Result[Total - 1] := S;
    end;
    ToSplit := ToSplit.Substring(P + Separator[Index].Length);
    P := ToSplit.IndexOfAny(Separator, Index);
  end;
网友评论