Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trim whitespace between characters

Tags:

How to remove whitespaces between characters in c#?

Trim() can be used to remove the empty spaces at the beginning of the string as well as at the end. For example " C Sharp ".Trim() results "C Sharp".

But how to make the string into CSharp? We can remove the space using a for or a for each loop along with a temporary variable. But is there any built in method in C#(.Net framework 3.5) to do this like Trim()?

like image 480
Thorin Oakenshield Avatar asked Oct 11 '10 10:10

Thorin Oakenshield


People also ask

How do I remove a space between two strings?

strip()—Remove Leading and Trailing Spaces. The str. strip() method removes the leading and trailing whitespace from a string.

How do I get rid of extra spaces in a text file?

Press Shift + Alt then press the down button before "56". Then backspace . You will see the cursor becomes big and then you can remove the spaces all at once.

How do you trim space between words in C#?

How to remove whitespaces between characters in c#? Trim() can be used to remove the empty spaces at the beginning of the string as well as at the end. For example " C Sharp ". Trim() results "C Sharp" .

What is the best way to remove whitespace characters from the start or end?

To remove whitespace characters from the beginning or from the end of a string only, you use the trimStart() or trimEnd() method.


1 Answers

You could use String.Replace method

string str = "C Sharp"; str = str.Replace(" ", ""); 

or if you want to remove all whitespace characters (space, tabs, line breaks...)

string str = "C Sharp"; str = Regex.Replace(str, @"\s", ""); 
like image 162
Julien Hoarau Avatar answered Oct 19 '22 03:10

Julien Hoarau