Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static string RemoveLineBreaks(string input)
- {
- const string singleWhiteSpace = " ";
- // normalize html tag e.g.: <I> => <i>
- var s = HtmlUtil.FixUpperTags(input);
- // unbreak line e.g: foo\r\nbar => foo bar
- s = s.Replace(Environment.NewLine, singleWhiteSpace);
- // ensure line was broke
- if (input.Length != s.Length)
- {
- // todo: remove!
- s = s.Replace("</i> <i>", singleWhiteSpace);
- s = s.Replace("</b> <b>", singleWhiteSpace);
- s = s.Replace("</u> <u>", singleWhiteSpace);
- var closingTag = string.Empty;
- var l = 0;
- for (int r = 0; r < s.Length; r++)
- {
- if (s[r] == '<')
- {
- l = r;
- }
- else if (s[r] == '>' && s[l] == '<' && s[l + 1] == '/') // closing tag
- {
- closingTag = s.Substring(l, r - l + 1);
- l = r + 1;
- }
- else if (s[r] == '>' && s[l] == '<' && closingTag.Length > 0) // opening tag (closing tag was found)
- {
- var openTag = s.Substring(l, r - l + 1); // todo: use span when in .net >= 6
- // todo: check if open-tag is pair with closing-tag
- // - true: remove
- // - false: continue
- if (openTag.IsPairWith(closingTag))
- {
- // todo: store the indexes that will be removed (in reverse)
- // - store close-tag index + len
- // - store open-tag index + len
- closingTag = string.Empty;
- }
- // else: continue looking for matching pair
- }
- else if (s[r] != ' ')
- {
- // reset closing tag | char found that is not whitespace or start tag (start tag are handled above)
- closingTag = string.Empty;
- }
- }
- s = s.Replace("> ", ">");
- return s.Replace(" </", "</").FixExtraSpaces().Trim();
- }
- return input;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement