如何从一个字符串中删除换行符? [英] How to remove new line characters from a string?
本文介绍了如何从一个字符串中删除换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串按以下格式
I have a string in the following format
string s = "This is a Test String.\n This is a next line.\t This is a tab.\n'
我想删除的所有出现\ñ
和 \ r
从上面的字符串。
I want to remove all the occurrences of \n
and \r
from the string above.
我已经试过字符串s = s.Trim(新的char [] {'\ N','\ r'});
,但它并没有帮助。
I have tried string s = s.Trim(new char[] {'\n', '\r'});
but it didn't help.
推荐答案
我喜欢用普通的前pressions。在这种情况下,你可以这样做:
I like to use regular expressions. In this case you could do:
string replacement = Regex.Replace(s, @"\t|\n|\r", "");
正前pressions不是在.NET世界最新流行的,因为他们是在动态语言,但它们提供大量的电力来操作字符串。
Regular expressions aren't as popular in the .NET world as they are in the dynamic languages, but they provide a lot of power to manipulate strings.
这篇关于如何从一个字符串中删除换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文