Last post Jun 13, 2014 03:09 PM by Jerry8989
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".
Jun 13, 2014 09:42 AM|rajesh93180|LINK
This worked for me..
string test = "THIS IS A TEST";
Response.Write(test.Replace(" ", " "));
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.
//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);
Jun 13, 2014 03:09 PM|Jerry8989|LINK