Search code examples
delphidelphi-7trim

How to trim any character (or a substring) from a string?


I use C# basically. There I can do:

string trimmed = str.Trim('\t');

to trim tabulation from the string str and return the result to trimmed.

In delphi7 I found only Trim, that trims spaces.

How can I achieve the same functionality?


Solution

  • This is a kind of procedure sometimes easier to create than to find where it lives :)

    function TrimChar(const Str: string; Ch: Char): string;
    var
      S, E: integer;
    begin
      S:=1;
      while (S <= Length(Str)) and (Str[S]=Ch) do Inc(S);
      E:=Length(Str);
      while (E >= 1) and (Str[E]=Ch) do Dec(E);
      SetString(Result, PChar(@Str[S]), E - S + 1);
    end;