2014-07-15 57 views
-2

我想弄清楚一個簡單的方法從字符串中刪除\ r \ n。如何從字符串中刪除 r n c#

示例: 文本=

我試圖 「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」

+0

的可能重複[我怎樣才能把 「\ r \ n」 在C#中的字符串?我可以使用regEx嗎?](http://stackoverflow.com/questions/1981947/how-can-i-remove-rn-from-a-string-in-c-can-i-use-a-regex) –

+0

使用'string.replace()' – Rahul

+1

你給'text.Replace()'調用某處的結果嗎?它不會在原地替換它 – zerkms

回答

15

讀取更好:

text = text.Replace(System.Environment.NewLine, string.Empty); 
2

你設定的返回值回變量?

text = text.Replace("\r\n", ""); 
2

它返回一個值。你需要說text = ...

text = text.Replace("\r\n", ""); 
1

你最好試試這個。

text = text.Replace("\r\n", ""); 

希望這項工作。

0

試試這個。

text = text .Replace("\\r\\n", ""); 

這對我的工作;)