You are here

Different Types of Trim Functions in Delphi

Submitted by Asif Nowaj, Last Modified on 2019-11-20

Trim removes leading and trailing spaces and controls characters from the given string. All these types of functions are available in SysUtils unit. There are different string types available in Delphi and overloaded functions are available too for them.

Function syntax:
function Trim(const S: string): string; overload;
function Trim(const S: WideString): WideString; overload;

Below are some customized useful trim functions.

Instead of spaces, if you want to strip of trailing occurrence of character present in the stripchars, use the below function. This function can be overloaded for WideString too. Declaration would be function TrimCharRight(s, CharsToTrim: WideString): WideString; If you are using both function then you write ‘overload’ on both the declarations. Definition of WideString would remain same except it will take WideString instead of String.

String Version:

function TrimCharRight(s, CharsToTrim: string): string;
var
  i, LengthOfS: integer;
begin
  Result := s;
  LengthOfS := Length(s);
  for i := LengthOfS downto 1 do
    if Pos(s[i], CharsToTrim) = 0 then
    begin
      if i < LengthOfS then Delete(Result, i + 1, LengthOfS - i);
      exit;
    end;
  Result := '';
end;

WideString Version:

function TrimCharRight(s, CharsToTrim: WideString): WideString;
var
  i, LengthOfS: integer;
begin
  Result := s;
  LengthOfS := Length(s);
  for i := LengthOfS downto 1 do
    if Pos(s[i], CharsToTrim) = 0 then
    begin
      if i < LengthOfS then Delete(Result, i + 1, LengthOfS - i);
      exit;
    end;
  Result := '';
end;

Instead of spaces, if you want to strip of leading occurrence of character present in the stripchars, use this function.

String Version:

function TrimCharLeft(s, CharsToTrim: string): string;
var
  i: integer;
begin
  Result := s;
  for i := 1 to Length(s) do
    if Pos(s[i], CharsToTrim) = 0 then
    begin
      if i > 1 then Delete(Result, 1, i - 1);
      exit;
    end;
  Result := '';
end;

WideString Version:

function TrimCharLeft(s, CharsToTrim: WideString): WideString;
var
  i: integer;
begin
  Result := s;
  for i := 1 to Length(s) do
    if Pos(s[i], CharsToTrim) = 0 then
    begin
      if i > 1 then Delete(Result, 1, i - 1);
      exit;
    end;
  Result := '';
end;

Instead of spaces, if you want to strip of leading and trailing occurrence of character present in the stripchars, use below function. If you need function then you have to use above two functions TrimCharRight and TrimCharLeft have to defined too.

String Version:

function TrimChar(s, CharsToTrim: string): string;
begin
  Result := TrimCharRight(TrimCharLeft(s, CharsToTrim), CharsToTrim);
end;

WideString Version:

function TrimChar(s, CharsToTrim: WideString): WideString;
begin
  Result := TrimCharRight(TrimCharLeft(s, CharsToTrim), CharsToTrim);
end;

Instead of space, if you want to trim any occurrence of character in any position of a string then use below function.
String Version:

function TrimCharAll(s, CharsToTrim: string): string;
var
  i: integer;
begin
  Result := s;
  for i := Length(Result) downto 1 do
    if Pos(Result[i], CharsToTrim) > 0 then
      Delete(Result, i, 1);
end;

WideString Version:

function TrimCharAll(s, CharsToTrim: WideString): WideString;
var
  i: integer;
begin
  Result := s;
  for i := Length(Result) downto 1 do
    if Pos(Result[i], CharsToTrim) > 0 then
      Delete(Result, i, 1);
end;


Discussion or Comment

If you have anything in mind to share, please bring it in the discussion forum here.

https://forum.everyething.com/delphi-f39/