简体   繁体   English

从 C# 中的字符串中删除换行符的最快方法是什么?

[英]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.

相关问题 在 C# 中连接三个文件的最快方法是什么? - What would be the fastest way to concatenate three files in C#? 在C#中,删除字符串[]或List中空白条目的最快方法是什么 <string> ? - In C#, what is the fastest way to remove blank entries in a string[] or List<string>? 在大型.NET字符串中计算换行符的最快方法是什么? - What is the fastest way to count newlines in a large .NET string? 删除c#中字符串中前导特殊字符的最快方法 - Fastest way to remove the leading special characters in string in c# C#从字符串中删除重复项的最快方法:拆分与循环 - C# fastest way to remove duplicates from string: Split vs. Loop 在C#中迭代字符串中单个字符的最快方法是什么? - What is the fastest way to iterate through individual characters in a string in C#? C#从加载的xml中删除换行符 - C# Remove newlines from xml loaded 从字符串中提取子字符串最快的方法是C#中的定界符? - What is the fastest way to extract a substring from a string, up to a delimiter in C#? 将宽行从Cassandra加载到C#的最快方法是什么? - What is the fastest way to load a wide row from Cassandra to C#? 从C#在SQL Server中插入记录的最快方法是什么 - What is the fastest way to insert record in SQL Server from C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM