I have a need to get rid of all line breaks that appear in my strings (coming from db). I do it using code below:
value.Replace(\"\\r\\n\", \"\").Replace(\"\\n\"
Here are some quick solutions with .NET regex:
s = Regex.Replace(s, @"\s+", ""); (\s matches any Unicode whitespace chars)s = Regex.Replace(s, @"[\s-[\r\n]]+", ""); ([\s-[\r\n]] is a character class containing a subtraction construct, it matches any whitespace but CR and LF)\p{Zs} (any horizontal whitespace but tab) and \t (tab) from \s: s = Regex.Replace(s, @"[\s-[\p{Zs}\t]]+", "");.Wrapping the last one into an extension method:
public static string RemoveLineEndings(this string value)
{
return Regex.Replace(value, @"[\s-[\p{Zs}\t]]+", "");
}
See the regex demo.