Removing carriage return and new-line from the end of a string in c#

后端 未结 12 1152
终归单人心
终归单人心 2020-12-02 13:57

How do I remove the carriage return character (\\r) and the new line character(\\n) from the end of a string?

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 14:31

    I use lots of erasing level

    String donen = "lots of stupid whitespaces and new lines and others..."
    
    //Remove multilines
    donen = Regex.Replace(donen, @"^\s+$[\r\n]*", "", RegexOptions.Multiline);
    //remove multi whitespaces
    RegexOptions options = RegexOptions.None;
    Regex regex = new Regex("[ ]{2,}", options);
    donen = regex.Replace(donen, " ");
    //remove tabs
    char tab = '\u0009';
    donen = donen.Replace(tab.ToString(), "");
    //remove endoffile newlines
    donen = donen.TrimEnd('\r', '\n');
    //to be sure erase new lines again from another perspective
    donen.Replace(Environment.NewLine, "");
    

    and now we have a clean one row

提交回复
热议问题