Coding string with a specific encoding

Is there a function in Delphi equivalent to Cocoa stringByTrimmingCharactersInSet?

I need to exclude all characters included in the encoding that are at the beginning or end of the string. There can be no one, one or more start or end lines ...

What would be the most efficient way to do this in Delphi?

+5
source share
2 answers

As far as I know, such an RTL function does not exist. But you can check out JclStringspart of the JCL project module , which include StrTrimCharsLeftand StrTrimCharsRight.

function StrTrimCharsLeft(const S: string; const Chars: TCharValidator): string; overload;
function StrTrimCharsLeft(const S: string; const Chars: array of Char): string; overload;
function StrTrimCharRight(const S: string; C: Char): string;
function StrTrimCharsRight(const S: string; const Chars: TCharValidator): string; overload;
function StrTrimCharsRight(const S: string; const Chars: array of Char): string; overload;
+6
source

As far as I know, RTL does not include such a feature. You can use regular expressions to fill in the gap:

function MyTrim(const Input: string; const TrimChars: string): string;
begin
  Result := TRegEx.Replace(Input, Format('^[%s]*', [TrimChars]), '');
  Result := TRegEx.Replace(Result, Format('[%s]*$', [TrimChars]), '');
end;

, , - .

+1

All Articles