How can I remove “\r\n” from a string in C#? Can I use a regular expression?

前端 未结 8 1654
粉色の甜心
粉色の甜心 2020-12-04 23:16

I am trying to persist string from an ASP.NET textarea. I need to strip out the carriage return line feeds and then break up whatever is left into a string array of 50 chara

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 00:00

    This splits the string on any combo of new line characters and joins them with a space, assuming you actually do want the space where the new lines would have been.

    var oldString = "the quick brown\rfox jumped over\nthe box\r\nand landed on some rocks.";
    var newString = string.Join(" ", Regex.Split(oldString, @"(?:\r\n|\n|\r)"));
    Console.Write(newString);
    
    // prints:
    // the quick brown fox jumped over the box and landed on some rocks.
    

提交回复
热议问题