如何从字符串c#中删除\ r \n

Slo*_*the 1 c# string

我试图找出一个简单的方法从字符串中删除\ r \n.

示例:text ="this.is.a.string.\ r \n \nthis.is.a.string\r \n"

我试过了:

text.Replace("\r\n", "")text.Replace("\r\n", string.Empty)但它不工作.\r\n还在字符串中......

结果应为:"this.is.a.string.this.is.a.string"

Luk*_*ton 20

读得更好:

text = text.Replace(System.Environment.NewLine, string.Empty);
Run Code Online (Sandbox Code Playgroud)


小智 5

.NET 中的字符串是不可变的,因此您无法更改现有字符串 - 只能创建一个新字符串。该Replace方法返回修改后的结果,即

text = text.Replace(System.Environment.NewLine, string.Empty);
text = JObject.Parse(text);
Run Code Online (Sandbox Code Playgroud)