How to remove spaces and newlines in a line

Sorry if they are not very practical for C # Asp.Net, I hope to make me understand. I have this situation.

string content = ClearHTMLTags(HttpUtility.HtmlDecode(e.Body)); content=content.Replace("\r\n", ""); content=content.Trim(); ((Post)sender).Description = content + "..."; 

I would make sure that the line contains no content or spaces (Trim), and neither carriage return with the line feed, I use the code above, but it does not work perfectly.

any suggestions

Thanks a lot Fabri

+6
source share
3 answers

You can remove all spaces with this regex

 content = Regex.Replace(content, @"\s+", string.Empty); 

what are whitespace from MSDN .

Btw you are mistaken Trim with removing spaces, in fact it is only removing spaces at the beginning and at the end of the line. If you want to replace all spaces and carige returns, use my regex.

+18
source

it should do it

 String text = @"hdjhsjhsdcj/sjksdc\t\r\n asdf"; string[] charactersToReplace = new string[] { @"\t", @"\n", @"\r", " " }; foreach (string s in charactersToReplace) { text = text.Replace(s, ""); } 
+4
source

simple change only you missed the @ symbol

 string content = ClearHTMLTags(HttpUtility.HtmlDecode(e.Body)); content=content.Replace(@"\r\n", ""); content=content.Trim(); ((Post)sender).Description = content + "..."; 
0
source

All Articles