Closed Thread
Results 1 to 3 of 3

Thread: How to remove empty space from string ?

  1. #1
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    How to remove empty space from string ?

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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    2710 is offline Programmer
    Join Date
    Sep 2008
    Posts
    108
    Rep Power
    0

    Re: How to remove empty space from string ?

    Delphi Basics : StringReplace command

    You can use the string replace function

  4. #3
    Firebird_38 is offline Programmer
    Join Date
    Aug 2008
    Posts
    126
    Rep Power
    0

    Re: How to remove empty space from string ?

    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:
    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.
    It depends on what you're trying to do, which you haven't indicated.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. string interrupted when there is a space
    By nerio in forum C and C++
    Replies: 3
    Last Post: 07-24-2011, 08:35 AM
  2. IRC...its empty?!
    By Warrior in forum The Lounge
    Replies: 11
    Last Post: 05-28-2011, 11:09 PM
  3. multiline string, endl, \n, line space
    By jackson6612 in forum C and C++
    Replies: 9
    Last Post: 04-22-2011, 04:16 PM
  4. Replies: 3
    Last Post: 08-12-2009, 08:00 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts