How to remove extra space between two words using C#? Consider:
"Hello World"
I want this to be manipulated as "Hello World"
.
In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.
Using regexes with "\s" and doing simple string. split()'s will also remove other whitespace - like newlines, carriage returns, tabs.
RegexOptions options = RegexOptions.None; Regex regex = new Regex(@"[ ]{2,}", options); tempo = regex.Replace(tempo, @" ");
or even:
myString = Regex.Replace(myString, @"\s+", " ");
both pulled from here
var text = "Hello World"; Console.WriteLine(String.Join(" ", text.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With