Hi ALL. My first threat =)
Does anyone know how to remove empty space from string ?
Thanks in advance![]()
Delphi Basics : StringReplace command
You can use the string replace function
I have a similar problem. I need to remove a full space from a rope!
No, seriously, use
Code:betterstring:=Trim(original); //chop spaces from beginning and end betterstring:=StringReplace(betterstring,#9,' ',[rfReplaceAll]); //turn tabs into spaces betterstring:=StringReplace(betterstring,#13,' ',[rfReplaceAll]); //turn cariage returns into spaces betterstring:=StringReplace(betterstring,#10,' ',[rfReplaceAll]); //turn line feeds into spaces repeat oldbetter:=betterstring; betterstring:=trim(StringReplace(original,' ',' ',[rfReplaceAll]); //turns double spaces (' ') into single spaces (' '). until betterstring=oldbetter; //repeat the 2 to 1 transform until there aren't any more doubles (until we didn't do anything and the old=the current)
I'm actually assuming the StringReplace isn't necessary, but that you really just want to crop using trim(...);
But this will turn any string with weird whitespace into a string with plain single whitespace.
To delete all whitespace go with:
It depends on what you're trying to do, which you haven't indicated.Code:function NoWhite(src:string):string; var n:Integer; const white=[' ',#9,#13,#10]; //set of all characters you want to eliminate, here it's the space, the tab, the cr and the lf chars. //You may just go with: [#0..' '] which is all chars up to and including space. You could also go with allowed chars instead //like in "white=[#0..#255] - ['a'..'z','A'..'Z','0'..'9']" This would mark everything not alphanumeric as "white" and delete it. //Or, instead of saying "delete it" I should say "skip it", as this function keeps adding allowed chars only. begin result:=''; for n:=1 to Length(src) do if not (src[n] in white) then result:=result+src[n]; end; begin betterstring:=NoWhite('This is NOT a good string.'#13#10'It has multiple lines, spaces and'#9' TABS.'); //will return 'ThisisNOTagoodstring.Ithasmultiplelines,spacesandTABS.' //the first example would return: //'This is NOT a good string. It has multiple lines, spaces and TABS.' end.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks