You can also try with the LINQ approach to remove mutliple spaces and add only a single space.
Sample Code:
//Your sample string
string strvalue ="THIS IS A TEST";
//Split the words based on white sapce
var list = strvalue.Split(' ').Where(s => !string.IsNullOrWhiteSpace(s));
//Join the values back and add a single space in between
strvalue = string.Join(" ", list);
Member
25 Points
158 Posts
Replace multiple spaces in a string in multiple places to just one space?
Jun 13, 2014 09:34 AM|Jerry8989|LINK
I have a multiline text box that I allow my users to enter or paste information that I upload to my database.
I want to replace an carriage returns to a single space and I'm doing that with this code
string sList = txtBulk.Text.Replace("\n", " ").Replace("\r\n", " ") ;
I need to also replace multiple spaces with a single space. How can I do this?
If a user enters:
"THIS IS A TEST" I need it to transform to "THIS IS A TEST".
Thank you
string
Participant
1644 Points
792 Posts
Re: Replace multiple spaces in a string in multiple places to just one space?
Jun 13, 2014 09:42 AM|rajesh93180|LINK
This worked for me..
string
Mark as answer if you find this post helpful.
All-Star
50831 Points
9895 Posts
Re: Replace multiple spaces in a string in multiple places to just one space?
Jun 13, 2014 01:46 PM|A2H|LINK
You can also try with the LINQ approach to remove mutliple spaces and add only a single space.
Sample Code:
string
Aje
My Blog | Dotnet Funda
Member
25 Points
158 Posts
Re: Replace multiple spaces in a string in multiple places to just one space?
Jun 13, 2014 03:09 PM|Jerry8989|LINK
Thank you
string