fabry19dice fabry19dice - 1 month ago 12
ASP.NET (C#) Question

How to remove spaces and newlines in a string

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 string does not contain content nor spaces (Trim) and neither carriage return with line feed, I'm using the above code inserted but it does not work great either

any suggestions??

Thank you very much
Fabry

Answer

You can remove all whitespaces with this regex

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

what are whitespace characters from MSDN.

Btw you are mistaking Trim with removing spaces, in fact it's only removing spaces at the begining and at the end of string. If you want to replace all spaces and carige returns use my regex.

Comments