views:

360

answers:

2

To add a whitespace seperator in a string we use String.Join().

My question:What(and how) do I have to remove that seperator.

The string's structure is the following "FF FF FF FF FF FF FF FF FF...."

How do I remove the white spaces?

+5  A: 

C# Has a function for it.

Function is String.Replace(oldstring, newString);

String.Replace(" ", "");
Dmitris
A slightly more readable version would be: String.Replace(" ", string.Empty); In the end, its the same.
phsr
remember that the string functions leave the original string untouched. So you need i.e. var s = "FF FF FF FF"; var s = s.Replace(" ", ""); (if I remember correctly). Even though I know this, I have forgotten it many times and wondered why my string didn't change, etc. :p
Svish
+1  A: 

I don't think you need to use LINQ for this. Just split on whitespace and then re-join using an empty string as the separator. This would be best if you had mixed whitespace -- tabs, newlines, etc.

var newStr = string.Join( string.Empty, str.Split() );

or replace the whitespace with the empty string (this would be the best if all the whitespace where the same character).

var newStr = string.Replace( " ", string.Empty );
tvanfosson