Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to remove empty space from string ?

string

  • Please log in to reply
2 replies to this topic

#1 delphiproblematic

delphiproblematic

    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 11 March 2010 - 11:03 AM

Hi ALL. My first threat =)
Does anyone know how to remove empty space from string ?
Thanks in advance :rolleyes:
  • 0

#2 2710

2710

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 110 posts

Posted 11 March 2010 - 12:41 PM

Delphi Basics : StringReplace command

You can use the string replace function
  • 0

#3 Firebird_38

Firebird_38

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts

Posted 18 March 2010 - 08:27 AM

I have a similar problem. I need to remove a full space from a rope!

No, seriously, use

 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:
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.

It depends on what you're trying to do, which you haven't indicated.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download