← Tutti gli articoli

Remove Blank Lines in a textbox

18 giugno 2013  · C# · Recipe  ·  41 visite

Remove Blank Lines in a textbox

  1. private   string  removeBlankLines(String text)  
  2. {  
  3.     // Remove all tabs characters  
  4.     String retText = text.Replace("\t"" ");  
  5.   
  6.     // If the first one or more lines are blank, remove them  
  7.     retText = Regex.Replace(retText, "(?<Text>.*)(?:(^\u0020*\r\n)(\u0020*\r\n\u0020*)*)""${Text}");  
  8.   
  9.     // Replace all occurrences of muliple new lines to single newlines  
  10.     retText = Regex.Replace(retText, "(?<Text>.*)(?:(\u0020*\r\n\u0020*){2,})""${Text}\r\n");  
  11.   
  12.     // Remove any trailing new lines or white space  
  13.     retText = Regex.Replace(retText, "(?<Text>.*)(?:(\u0020*\r\n)(\u0020*\r\n\u0020*)*$)""${Text}");  
  14.   
  15.     return retText;  
  16. }