none
How to remove spaces in string RRS feed

  • Question

  •  

           I need to remove spaces in a string.

    Example:

                  I am getting value from UI like this "Hello  World "

         But I need value like this "HelloWorld".

     

     

     

    Wednesday, March 14, 2007 8:38 AM

Answers

All replies

  • This should do the trick:

    string str = "Hello World".Replace(" ", "");

    Wednesday, March 14, 2007 9:24 AM
  • string value = "Hello World";
    string result = value.Replace(" ", "");

    That should do it. I hope it helps

    Wednesday, March 14, 2007 9:26 AM
  •  

             I have tried as per your suggestion.But I am getting the following error.

         "The best overload method match for 'String.Remove(int,int)' has some invalid arguments "

     

    Wednesday, March 14, 2007 12:54 PM
  • Are you sure you are using String.Replace()? 'Cause that error message indicates you use String.Remove(). Check again.
    Wednesday, March 14, 2007 1:40 PM
  • try this:

    string str = "Hello World";
    string str2 = str.Replace(" ", "");

    Wednesday, March 14, 2007 1:40 PM
  • thats because have to use someStr.replace(" ","") and not someStr.remove()
    Wednesday, March 14, 2007 1:49 PM