如何求字符串的字串??

解决方案 »

  1.   

    {-------------------------------------------------------------------------------
    *GetChars - Returns characters as specified by aSet. (Purges characters not in set)
     Example: GetChars('123Hallo',['a'..'z']) > 'allo'
     Example: GetChars('123Hallo',['0'..'9']) > '123'
    See also CountChars, Purge
    -------------------------------------------------------------------------------}
    function TStringManager.GetChars(const s : String; const aSet : TCharSet) : String;
    var  i : Integer;
      RLen : Integer;
    begin
      RLen := 0;
      //We first calculate the length to avoid repeated mem allocation
      for i := 1 to Length(s) do if s[i] in aSet then Inc(RLen);
      SetLength(Result, RLen);
      if RLen=0 then Exit;
      RLen := 0;
      for i := 1 to Length(s) do if s[i] in aSet then
      begin
        Inc(RLen);
        Result[RLen] := s[i];
      end;
    end;{-------------------------------------------------------------------------------
    *GetChars- Returns only characters from s found in aStrSet.
     Example: GetChars('123Hallo123','1a') > '1a1'
    See also GetChars, CountChars, Purge
    -------------------------------------------------------------------------------}
    function TStringManager.GetChars(const s : String; const aStrSet : String) : String;
    begin
      Result := GetChars(s, ToSet(aStrSet));
    end;