Bv202
Bv202

Reputation: 4054

Removing line breaks doesn't work

So I have this method:

public static string ToProperText(this HtmlHelper helper, string text)
{
    System.Diagnostics.Debug.WriteLine(text);
    System.Diagnostics.Debug.WriteLine(text.Replace("\r\n", ""));

    string lineSeparator = ((char)0x2028).ToString();
    string paragraphSeparator = ((char)0x2029).ToString();

    System.Diagnostics.Debug.WriteLine(text.Replace("\r\n", string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty).Replace(lineSeparator, string.Empty).Replace(paragraphSeparator, string.Empty));
    System.Diagnostics.Debug.WriteLine(text.Replace("a", ""));
    return null;
}

When called with some data from the database, this is the output:

<p>\r\n Vanaf nu worden de websiteberichten ook <u>automatisch</u> in de Nieuwssectie op het <strong>forum</strong> geplaatst.</p>\r\n
<p>\r\n Vanaf nu worden de websiteberichten ook <u>automatisch</u> in de Nieuwssectie op het <strong>forum</strong> geplaatst.</p>\r\n
<p>\r\n Vanaf nu worden de websiteberichten ook <u>automatisch</u> in de Nieuwssectie op het <strong>forum</strong> geplaatst.</p>\r\n
<p>\r\n Vnf nu worden de websiteberichten ook <u>utomtisch</u> in de Nieuwssectie op het <strong>forum</strong> gepltst.</p>\r\n

No matter what I do, the \r\n won't get removed from the string, although other replacements do work. Any idea what is going on here?

Thanks

Upvotes: 1

Views: 460

Answers (3)

Terry Seidler
Terry Seidler

Reputation: 2043

Guessing here.. have you tried:

text.Replace("\\r\\n", "")?

"\r\n" would replace real line breaks, not the actual text '\r\n':

What character escape sequences are available?

Alternatively, duluca's option should work too.

Upvotes: 2

user610217
user610217

Reputation:

the string is probably escaped already. Try

text.Replace("\\r\\n")

Upvotes: 2

Doguhan Uluca
Doguhan Uluca

Reputation: 7333

Try

System.Diagnostics.Debug.WriteLine(text.Replace(@"\r\n", ""));

Look for the added @ symbol.

Upvotes: 1

Related Questions