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

I use C # in principle. There I can do:

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

to trim tab from string str and return result to trimmed .

In delphi7, I found only Trim , which cuts spaces.

How can I achieve the same functionality?

+4
source share
6 answers

This is a kind of procedure, which is sometimes easier to create than to find where she 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; 
+5
source

There is a helper parameter, TStringHelper.Trim , that takes a Char array as an optional parameter.

 function Trim(const TrimChars: array of Char): string; overload; 

So you can use

 trimmed := str.Trim([#09]); 

for your example. #09 there is an ASCII code for the Tab character.

This feature exists since at least Delphi XE3.

Hope this helps.

+8
source

In Delphi, the Trim function does not accept parameters, but trims other characters, as well as spaces. Here is the code (from System.SysUtils to XE2, I donโ€™t think it has changed):

 function Trim(const S: string): string; var I, L: Integer; begin L := Length(S); I := 1; if (L > 0) and (S[I] > ' ') and (S[L] > ' ') then Exit(S); while (I <= L) and (S[I] <= ' ') do Inc(I); if I > L then Exit(''); while S[L] <= ' ' do Dec(L); Result := Copy(S, I, L - I + 1); end; 

It trims anything smaller than `` '' which eliminates any control characters such as tab, carriage return, and line feed.

+3
source

Delphi does not provide a function that does what you want. The built-in Trim function always truncates the same set of characters (spaces and control characters) from both ends of the input string. A few answers here show a basic method for trimming arbitrary characters. As you can see, this should not be difficult. Here is my version:

 function Trim(const s: string; c: Char): string; var First, Last: Integer; begin First := 1; Last := Length(s); while (First <= Last) and (s[First] = c) do Inc(First); while (First < Last) and (s[Last] = c) do Dec(last); Result := Copy(s, First, Last - First + 1); end; 

To adapt this to trim multiple characters, all you have to do is change the second conditional term in each loop. What you change depends on how you choose to represent multiple characters. C # uses an array. You can also put all characters in a string, or you can use Delphi's own set type.

 function Trim(const s: string; const c: array of Char): string; // Replace `s[x] = c` with `CharInArray(s[x], c)`. function Trim(const s: string; const c: string): string; // Replace `s[x] = c` with `CharInString(s[x], s)`. function Trim(const s: string; const c: TSysCharSet): string; // Replace `s[x] = c` with `s[x] in c`. 

The CharInArray and CharInString easy to write:

 function CharInArray(c: Char; ar: array of Char): Boolean; var i: Integer; begin Result := True; for i := Low(ar) to High(ar) do if ar[i] = c then exit; Result := False; end; // CharInString is identical, except for the type of `ar`. 

Recall that with Delphi 2009, Char is an alias for WideChar , which means that it is too large to fit into the set, so you cannot use the installed version unless you are guaranteed that the input will always fit into AnsiChar . In addition, the s[x] in c syntax generates warnings for WideChar arguments, so you should use CharInSet(s[x], c) instead. (Unlike CharInArray and CharInString , RTL provides CharInSet for versions of Delphi that need it.)

+2
source

You can use StringReplace:

 var str:String; begin str:='The_aLiEn'+Chr(VK_TAB)+'Delphi'; ShowMessage(str); str:=StringReplace(str, chr(VK_Tab), '', [rfReplaceAll]); ShowMessage(str); end; 

Disables all Tab characters from the given string. But you can improve it if you want to remove the leading and trailing tabs, you can also use the Pos function.

Edit: For a comment asking how to do this with Pos, here it is:

 var str:String; s, e: PChar; begin str:=Chr(VK_TAB)+Chr(VK_TAB)+'The_aLiEn'+Chr(VK_TAB)+'Delphi'+Chr(VK_TAB)+Chr(VK_TAB); s:=PChar(str); while Pos(Chr(VK_TAB), s)=1 do inc(s); e:=s; inc(e, length(s)-1); while Pos(Chr(VK_TAB), e)=1 do dec(e); str:=Copy(s, 1, length(s)-length(e)+1); ShowMessage(str); end; 

This, of course, is the same approach of Maxey and a little more work than him. But if you do not have much time to complete the work, and if Pos is what you thought in the first place, then this can be done. You, the programmer, should and should think about optimization, not me. And if we talk about the limitations of optimization, with a little tweak to replace Pos with char, this will work faster than Maxeyโ€™s code.

Edit to generalize Substr search:

 function TrimStr(const Source, SubStr: String): String; var s, e: PChar; l: Integer; begin s:=PChar(Source); l:=Length(SubStr); while Pos(SubStr, s)=1 do inc(s, l); e:=s; inc(e, length(s)-l); while Pos(SubStr, e)=1 do dec(e, l); Result:=Copy(s, 1, length(s)-length(e)+l); end; 
+1
source

JEDI JCL v2.7 provides these useful features for what you need:

function StrTrimCharLeft(const S: string; C: Char): string; 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; function StrTrimQuotes(const S: string): string;

0
source

Source: https://habr.com/ru/post/1447396/


All Articles