← Tutti gli articoli
Remove Blank Lines in a textbox
18 June 2013 ·
C# · Recipe ·
33 visite
Remove Blank Lines in a textbox
- private string removeBlankLines(String text)
- {
- // Remove all tabs characters
- String retText = text.Replace("\t", " ");
- // If the first one or more lines are blank, remove them
- retText = Regex.Replace(retText, "(?<Text>.*)(?:(^\u0020*\r\n)(\u0020*\r\n\u0020*)*)", "${Text}");
- // Replace all occurrences of muliple new lines to single newlines
- retText = Regex.Replace(retText, "(?<Text>.*)(?:(\u0020*\r\n\u0020*){2,})", "${Text}\r\n");
- // Remove any trailing new lines or white space
- retText = Regex.Replace(retText, "(?<Text>.*)(?:(\u0020*\r\n)(\u0020*\r\n\u0020*)*$)", "${Text}");
- return retText;
- }