public static string RemoveLineBreaks(string s) { s = HtmlUtil.FixUpperTags(s); s = s.Replace(Environment.NewLine + "</i>", "</i>" + Environment.NewLine); s = s.Replace(Environment.NewLine + "</b>", "</b>" + Environment.NewLine); s = s.Replace(Environment.NewLine + "</u>", "</u>" + Environment.NewLine); s = s.Replace(Environment.NewLine + "</font>", "</font>" + Environment.NewLine); s = s.Replace("</i> " + Environment.NewLine + "<i>", " "); s = s.Replace("</i>" + Environment.NewLine + " <i>", " "); s = s.Replace("</i>" + Environment.NewLine + "<i>", " "); s = s.Replace(Environment.NewLine, " "); s = s.Replace(" </i>", "</i> "); s = s.Replace(" </b>", "</b> "); s = s.Replace(" </u>", "</u> "); s = s.Replace(" </font>", "</font> "); s = FixExtraSpaces(s); return(s.Trim()); }
public static string RemoveSpaceBeforeAfterTag(string text, string openTag) { text = HtmlUtil.FixUpperTags(text); var closeTag = string.Empty; switch (openTag) { case "<i>": closeTag = "</i>"; break; case "<b>": closeTag = "</b>"; break; case "<u>": closeTag = "</u>"; break; } if (closeTag.Length == 0 && openTag.Contains("<font ", StringComparison.Ordinal)) { closeTag = "</font>"; } // Open tags var open1 = openTag + " "; var open2 = Environment.NewLine + openTag + " "; var open3 = openTag + Environment.NewLine; // Closing tags var close1 = "! " + closeTag + Environment.NewLine; var close2 = "? " + closeTag + Environment.NewLine; var close3 = " " + closeTag; var close4 = " " + closeTag + Environment.NewLine; var close5 = Environment.NewLine + closeTag; if (text.Contains(close1, StringComparison.Ordinal)) { text = text.Replace(close1, "!" + closeTag + Environment.NewLine); } if (text.Contains(close2, StringComparison.Ordinal)) { text = text.Replace(close2, "?" + closeTag + Environment.NewLine); } if (text.EndsWith(close3, StringComparison.Ordinal)) { text = text.Substring(0, text.Length - close3.Length) + closeTag; } if (text.Contains(close4)) { text = text.Replace(close4, closeTag + Environment.NewLine); } // e.g: ! </i><br>Foobar if (text.StartsWith(open1, StringComparison.Ordinal)) { text = openTag + text.Substring(open1.Length); } // e.g.: <i>\r\n if (text.StartsWith(open3, StringComparison.Ordinal)) { text = text.Remove(openTag.Length, Environment.NewLine.Length); } // e.g.: \r\n</i> if (text.EndsWith(close5, StringComparison.Ordinal)) { text = text.Remove(text.Length - openTag.Length - Environment.NewLine.Length - 1, Environment.NewLine.Length); } if (text.Contains(open2, StringComparison.Ordinal)) { text = text.Replace(open2, Environment.NewLine + openTag); } // Hi <i> bad</i> man! -> Hi <i>bad</i> man! text = text.Replace(" " + openTag + " ", " " + openTag); text = text.Replace(Environment.NewLine + openTag + " ", Environment.NewLine + openTag); // Hi <i>bad </i> man! -> Hi <i>bad</i> man! text = text.Replace(" " + closeTag + " ", closeTag + " "); text = text.Replace(" " + closeTag + Environment.NewLine, closeTag + Environment.NewLine); text = text.Trim(); if (text.StartsWith(open1, StringComparison.Ordinal)) { text = openTag + text.Substring(open1.Length); } return(text); }