• 分割字符串 ExtractStrings


    //分割字符串 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;

  • 相关阅读:
    监视和调整硬件性能
    ASP.NET MVC三个重要的描述对象:ActionDescriptor
    REST in Practice
    软硬件错误的排查之道
    OMCS 多媒体连接系统
    逻辑层 vs 物理层
    深入浅出裸测之道单元测试的单元化
    简单的网络爬虫实现
    WCF返回JSON与传入JSON(普通参数或对象)
    .NET程序员的一个礼物——TypeMonster
  • 原文地址:https://www.cnblogs.com/prtmon/p/2948162.html
Copyright © 2020-2023  润新知