21

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

fabry19dice
  • 331
  • 1
  • 3
  • 9

3 Answers3

45

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.

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
6

this 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, "");
        }
Ehsan
  • 31,833
  • 6
  • 56
  • 65
1

simple change only you missed @ symbol

string content = ClearHTMLTags(HttpUtility.HtmlDecode(e.Body));
content=content.Replace(@"\r\n", "");
content=content.Trim();
((Post)sender).Description = content + "...";    
wonea
  • 4,783
  • 17
  • 86
  • 139
vino20
  • 429
  • 4
  • 13