Removing all whitespace lines from a multi-line string efficiently

前端 未结 19 1982
名媛妹妹
名媛妹妹 2020-12-29 04:25

In C# what\'s the best way to remove blank lines i.e., lines that contain only whitespace from a string? I\'m happy to use a Regex if that\'s the best solution.

EDIT

相关标签:
19条回答
  • 2020-12-29 04:59

    Try this.

    string s = "Test1" + Environment.NewLine + Environment.NewLine + "Test 2";
    Console.WriteLine(s);
    
    string result = s.Replace(Environment.NewLine, String.Empty);
    Console.WriteLine(result);
    
    0 讨论(0)
提交回复
热议问题