Removing all whitespace lines from a multi-line string efficiently

前端 未结 19 2155
名媛妹妹
名媛妹妹 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:44

    Im not sure is it efficient but =)

      List strList = myString.Split(new string[] { "\n" }, StringSplitOptions.None).ToList();
      myString = string.Join("\n", strList.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList());
    

提交回复
热议问题