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\"
Have you tried string.Replace(Environment.NewLine, "")
? That usually gets a lot of them for me.
8232 (0x2028) and 8233 (0x2029) are the only other ones you might want to eliminate. See the documentation for char.IsSeparator.
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.
Check out this link: http://msdn.microsoft.com/en-us/library/844skk0h.aspx
You wil lhave to play around and build a REGEX expression that works for you. But here's the skeleton...
static void Main(string[] args)
{
StringBuilder txt = new StringBuilder();
txt.Append("Hello \n\n\r\t\t");
txt.Append( Convert.ToChar(8232));
System.Console.WriteLine("Original: <" + txt.ToString() + ">");
System.Console.WriteLine("Cleaned: <" + CleanInput(txt.ToString()) + ">");
System.Console.Read();
}
static string CleanInput(string strIn)
{
// Replace invalid characters with empty strings.
return Regex.Replace(strIn, @"[^\w\.@-]", "");
}
Below is the extension method solving my problem. LineSeparator and ParagraphEnding can be of course defined somewhere else, as static values etc.
public static string RemoveLineEndings(this string value)
{
if(String.IsNullOrEmpty(value))
{
return value;
}
string lineSeparator = ((char) 0x2028).ToString();
string paragraphSeparator = ((char)0x2029).ToString();
return value.Replace("\r\n", string.Empty)
.Replace("\n", string.Empty)
.Replace("\r", string.Empty)
.Replace(lineSeparator, string.Empty)
.Replace(paragraphSeparator, string.Empty);
}
personally i'd go with
public static String RemoveLineEndings(this String text)
{
StringBuilder newText = new StringBuilder();
for (int i = 0; i < text.Length; i++)
{
if (!char.IsControl(text, i))
newText.Append(text[i]);
}
return newText.ToString();
}