using System.Diagnostics;
using System.Text.RegularExpressions;
public static class Program
public static string RemoveAllWhitespace(this string str)
var src = str.ToCharArray();
for (var i = 0; i < len; i++)
if (!char.IsWhiteSpace(ch) && ch != '\0')
return new string(src, 0, dstIdx);
public static string TrimStartUnicode(this string str)
var src = str.ToCharArray();
for (var i = 0; i < len; i++)
if (!char.IsWhiteSpace(ch) && !char.IsControl(ch) )
return new string(src, 0, dstIdx);
private static readonly char[] TrimNewLineChars = Environment.NewLine.ToCharArray();
public static string RemoveEmptyLines(this string str)
var lines = str.Split(TrimNewLineChars, StringSplitOptions.RemoveEmptyEntries);
var sb = new StringBuilder(str.Length);
foreach (var line in lines)
if (!String.IsNullOrWhiteSpace(line))
public static String RemoveAllBlankLinesIssue(this string value)
StringBuilder output = new StringBuilder(value.Length);
using (StringReader sr = new StringReader(value))
while ((line = sr.ReadLine()) != null)
if (line == '\0'.ToString())
Console.WriteLine("is string '\\0' empty or null = " + string.IsNullOrEmpty('\0'.ToString()));
Console.WriteLine("is string '\\0' whitespace or null = " + String.IsNullOrWhiteSpace('\0'.ToString()));
Console.WriteLine("is char '\\0' char.IsWhiteSpace = " + char.IsWhiteSpace('\0'));
Console.WriteLine("is char '\\0' char.IsControl = " + char.IsControl('\0'));
if (line.Contains('\u0080'.ToString()))
Console.WriteLine("is string '\\0080' empty or null = " + string.IsNullOrEmpty('\u0080'.ToString()));
Console.WriteLine("is string '\\0080' whitespace or null = " + String.IsNullOrWhiteSpace('\u0080'.ToString()));
if (!String.IsNullOrWhiteSpace(line) && !string.IsNullOrEmpty(line))
return output.ToString();
public static String RemoveAllBlankLinesFinal(this string value)
StringBuilder output = new StringBuilder(value.Length);
using (StringReader sr = new StringReader(value))
while ((line = sr.ReadLine()) != null)
temp = line.TrimStartUnicode();
if (!string.IsNullOrWhiteSpace(temp) && !string.IsNullOrEmpty(temp))
return output.ToString();
public static String RemoveAllBlankLinesRegex(this string s)
return Regex.Replace(s, @"^\s+$[\r\n]*", string.Empty, RegexOptions.Multiline);
public static void Main()
string output = string.Empty;
Stopwatch sw = new Stopwatch();
string emptytest = "Tell me and I forget.\n \n \nTeach me and I remember. \r\n \r\n\r\nInvolve me and I learn. \r \r\r\0\r\r Pad Unicode \\u0080 next line\n\n\u0080\r\rby Benjamin Franklin.\r\n";
output = emptytest.RemoveEmptyLines();
Console.WriteLine(output + " in " + sw.ElapsedTicks + " ticks");
Console.WriteLine("-------- StringReader Issue ----------------");
output = emptytest.RemoveAllBlankLinesIssue();
Console.WriteLine(output + " in " + sw.ElapsedTicks + " ticks");
Console.WriteLine("-------- StringReader Final ----------------");
output = emptytest.RemoveAllBlankLinesFinal();
Console.WriteLine(output + " in " + sw.ElapsedTicks + " ticks");
Console.WriteLine("-------- Regex ----------------");
output = emptytest.RemoveAllBlankLinesRegex();
Console.WriteLine(output + "\n in " + sw.ElapsedTicks + " ticks");