public static string NormalizeSpace(string input, NormalizeSpaceOptions options = NormalizeSpaceOptions.Default) { string pattern = options.HasFlag(NormalizeSpaceOptions.PreserveLineBreaks) ? @"[^\S\r\n]+" : @"\s+"; return(Regex.Replace(input, pattern, " ")); }
public static string NormalizeLineBreaks(string input, NormalizeSpaceOptions options = NormalizeSpaceOptions.Default) { string result = Regex.Replace(input, @"\r\n|\n", "\n"); // Runs of 2 or more line breaks will be collapsed into a single line break unless the PreserveLineBreaks flag is set. if (!options.HasFlag(NormalizeSpaceOptions.PreserveLineBreaks)) { if (options.HasFlag(NormalizeSpaceOptions.PreserveParagraphBreaks)) { result = Regex.Replace(result, @"\n{3,}", "\n\n"); } else { result = Regex.Replace(result, @"\n+", "\n"); } } // Replace the temporary newlines with the evironment's newline sequence. result = Regex.Replace(result, @"\n", Environment.NewLine); return(result); }