What would be the fastest way to remove Newlines f

2019-06-15 15:40发布

I have a string that has some Environment.Newline in it. 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?

5条回答
倾城 Initia
2楼-- · 2019-06-15 16:19

Don't reinvent the wheel - just use myString.Replace(Environment.NewLine, ",")

查看更多
Animai°情兽
3楼-- · 2019-06-15 16:24

Why not:

string s = "foobar\ngork";
string v = s.Replace(Environment.NewLine,",");
System.Console.WriteLine(v);
查看更多
Deceive 欺骗
4楼-- · 2019-06-15 16:24

The best way is the builtin way: Use string.Replace. Why do you need alternatives?

查看更多
戒情不戒烟
5楼-- · 2019-06-15 16:34

Like this:

string s = "hello\nworld";
s = s.Replace(Environment.NewLine, ",");
查看更多
▲ chillily
6楼-- · 2019-06-15 16:39
string sample = "abc" + Environment.NewLine + "def";
string replaced = sample.Replace(Environment.NewLine, ",");
查看更多
登录 后发表回答