Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a method for removing whitespace characters from a string?

Is there a string class member function (or something else) for removing all spaces from a string? Something like Python's str.strip() ?

like image 373
ApprenticeHacker Avatar asked Sep 12 '11 04:09

ApprenticeHacker


1 Answers

You could simply do:

myString = myString.Replace(" ", "");

If you want to remove all white space characters you could use Linq, even if the syntax is not very appealing for this use case:

myString = new string(myString.Where(c => !char.IsWhiteSpace(c)).ToArray());
like image 178
BrokenGlass Avatar answered Sep 23 '22 13:09

BrokenGlass