Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove whitespace from a string in C#?

Tags:

c#

Any one know how to remove excessive whitespace from a string?

For example:

string myString = "I          am going to work.";

Anyone know a good script for doing this kind of trimming?

like image 286
Edward Avatar asked Sep 02 '25 15:09

Edward


2 Answers

Regex.Replace(myString, @"\s+", " ") would do it.

like image 56
joce Avatar answered Sep 05 '25 04:09

joce


Regex.Replace(myString, @"\s+", " ") ought to do it.

like image 28
Mark Sowul Avatar answered Sep 05 '25 06:09

Mark Sowul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!