How to remove the blank spaces in a string?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: How to remove the blank spaces in a string?

  1. #1
    Join Date
    Jun 2004
    Posts
    12

    How to remove the blank spaces in a string?

    Hi,

    I have a string, say for example string strTemp = "Hello how are you?".
    Now how can i remove all the blank spaces in the string?

    I tried using strTemp.Replace(" ","");
    But this doesn't help...
    Any idea how this can be done?

    regards,
    buddy

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    The string manipulation functions return strings and leave the original intact.

    Therefore :

    Code:
    string sHello = "Hello there you people";
    sHello = sHello.Replace(" " , "");
    This works.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Jun 2004
    Posts
    12
    Thanks Darwen

  4. #4
    Join Date
    Mar 2003
    Location
    Japan
    Posts
    120
    There is a good method in string's method, String.Trim().

    It removes all space from the string.
    you can also use "Hello how are you?".Trim();
    Ee.... Sugoi Ne~~

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940
    Trim() removes all whitespace characters from the start and the end of the string - not all the spaces in the string.

    "Hello there you people" trimmed = "Hello there you people"
    " Hello there " trimmed = "Hello there".

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  6. #6
    Join Date
    Dec 2010
    Posts
    1

    Re: How to remove the blank spaces in a string?

    how do you do this in C?

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to remove the blank spaces in a string?

    You ask in a C forum.

  8. #8
    Join Date
    Jul 2013
    Posts
    2

    Re: How to remove the blank spaces in a string?

    Am trying to clear the blank spaces in between the text which looks like this.

    go



    go


    is there a way we can trim the blank spaces.

    tried using text.replace(" ","") - did not work.

  9. #9
    Join Date
    Jul 2013
    Posts
    2

    Re: How to remove the blank spaces in a string?

    FOund a solution works fine

    random.Text = Regex.Replace(random.Text, @"((?<=^|\n)\s*\n)|(\s*$)", string.Empty);

    This trims away all the blank spaces present in the text or a string.
    Thanks

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,363

    Re: How to remove the blank spaces in a string?

    Quote Originally Posted by jakejoker View Post
    tried using text.replace(" ","") - did not work.
    Post #2 explained why text.Replace() doesn't work as you have it coded.

    Actually it does work, but you need to reassign the Replace() method back to the string. So the following will work:
    Code:
    text = text.Replace(" ", "");

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center