![](/img/trans.png)
[英]How can I remove "\r\n" from a string in C#? Can I use a regular expression?
[英]How do I remove \r\n from string in C#?
我試圖找出一種簡單的方法從字符串中刪除 \\r\\n。
示例: text = "this.is.a.string.\\r\\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”
這讀起來更好:
text = text.Replace(System.Environment.NewLine, string.Empty);
.NET 中的字符串是不可變的,因此您無法更改現有字符串 - 您只能創建一個新字符串。 Replace
方法返回修改后的結果,即
text = text.Replace(System.Environment.NewLine, string.Empty);
text = JObject.Parse(text);
您是否將返回值設置回變量?
text = text.Replace(@"\r\n", "");
它返回一個值。 你需要說text = ...
text = text.Replace(@"\r\n", "");
嘗試這個:
text = text.Replace(System.Environment.NewLine, "");
你最好試試這個。
text = text.Replace("\r\n", "");
希望這項工作。
嘗試這個。
text = text .Replace("\\r\\n", "");
這對我有用;)
有時您無法替換JSON字符串中的新行。 以下代碼將消除所有機會。
json = json.Replace(Environment.NewLine, "").Replace("\r\n", "").Replace("\r", "").Replace("\n", "").Replace("\\r", "").Replace("\\n", "");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.