[英]What would be the fastest way to remove Newlines from a String in C#?
I have a string that has some Environment.Newline in it.我有一个字符串,其中包含一些 Environment.Newline。 I'd like to strip those from the string and instead, replace the Newline with something like a comma.我想从字符串中删除它们,而是用逗号之类的东西替换换行符。
What would be, in your opinion, the best way to do this using C#.NET 2.0?在您看来,使用 C#.NET 2.0 执行此操作的最佳方法是什么?
Why not:为什么不:
string s = "foobar\ngork";
string v = s.Replace(Environment.NewLine,",");
System.Console.WriteLine(v);
Like this:像这样:
string s = "hello\nworld";
s = s.Replace(Environment.NewLine, ",");
string sample = "abc" + Environment.NewLine + "def";
string replaced = sample.Replace(Environment.NewLine, ",");
不要重新发明轮子 - 只需使用:
myString.Replace(Environment.NewLine, ",")
The best way is the builtin way: Use string.Replace
.最好的方法是内置方式:使用string.Replace
。 Why do you need alternatives?为什么需要替代品?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.