How to remove extra white space (more than one space) from a string in vb or C# using Regex. in .NET


To remove the unwanted whitespace, use the regex like so

Dim str As String = "Some    string that    contains   too many    spaces"

Regex.Replace(str, "\s{2,}", " ")

 

*Note - the {2,} can be changed to your desired number to omit 2,3, or however many white spaces you want

Tags: Regex
Last update:
2015-05-12 20:34
Author:
Mark
Revision:
1.0
Average rating:0 (0 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.

Records in this category

Tags