C# 向字符串添加回车
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10096100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add carriage return to a string
提问by
I have a long string.
我有一个很长的字符串。
string s1 = "'99024','99050','99070','99143','99173','99191','99201','99202','99203','99204','99211','99212','99213','99214','99215','99217','99218','99219','99221','99222','99231','99232','99238','99239','99356','99357','99371','99374','99381','99382','99383','99384','99385','99386','99391','99392'";
I want
我想要
string s2 =
"'99024',
'99050',
'99070',
'99143',
'99173',
'99191',
'99201',
'99202',....";
In other words. Maybe it likes:
换句话说。也许它喜欢:
string s2 = "'99024',"+'\n'+"'99050',"+'\n'+"'99070',"+'\n'+"'99143',"+'\n'+.....;
I need a concise code. Maybe LINQ. Thanks.
我需要一个简洁的代码。也许是 LINQ。谢谢。
采纳答案by Servy
string s2 = s1.Replace(",", ",\n");
回答by Dan Rigby
string s2 = s1.Replace(",", "," + Environment.NewLine);
Also, just from a performance perspective, here's how the three current solutions I've seen stack up over 100k iterations:
此外,仅从性能的角度来看,以下是我看到的三个当前解决方案累积超过 10 万次迭代的方式:
ReplaceWithConstant - Ms: 328, Ticks: 810908
ReplaceWithEnvironmentNewLine - Ms: 310, Ticks: 766955
SplitJoin - Ms: 483, Ticks: 1192545
ReplaceWithConstant:
替换为常量:
string s2 = s1.Replace(",", ",\n");
ReplaceWithEnvironmentNewLine:
ReplaceWithEnvironmentNewLine:
string s2 = s1.Replace(",", "," + Environment.NewLine);
SplitJoin:
拆分连接:
string s2 = String.Join("," + Environment.NewLine, s1.Split(','));
ReplaceWithEnvironmentNewLineand ReplaceWithConstantare within the margin of error of each other, so there's functionally no difference.
ReplaceWithEnvironmentNewLine和ReplaceWithConstant在彼此的误差范围内,因此在功能上没有区别。
Using Environment.NewLineshould be preferred over "\n"for the sake readability and consistency similar to using String.Emptyinstead of "".
为了可读性和一致性,使用Environment.NewLine应该优先"\n"于使用String.Empty而不是使用""。
回答by Anders Abel
string s2 = s1.Replace(",", ",\n") + ",....";
回答by Dan Rigby
Another option:
另外一个选项:
string s2 = String.Join("," + Environment.NewLine, s1.Split(','));
回答by Dustin Koeller
Environment.NewLine should be used as Dan Rigby said but there is one problem with the String.Empty. It will remain always empty no matter if it is read before or after it reads. I had a problem in my project yesterday with that. I removed it and it worked the way it was supposed to. It's better to declare the variable and then call it when it's needed. String.Empty will always keep it empty unless the variable needs to be initialized which only then should you use String.Empty. Thought I would throw this tid-bit out for everyone as I've experienced it.
Environment.NewLine 应该像 Dan Rigby 所说的那样使用,但 String.Empty 存在一个问题。无论是在读取之前还是之后读取,它都将始终为空。我昨天在我的项目中遇到了这个问题。我删除了它,它按照预期的方式工作。最好先声明变量,然后在需要时调用它。String.Empty 将始终保持为空,除非需要初始化变量,然后才应该使用 String.Empty。以为我会像我经历过的那样为每个人抛出这个花絮。
回答by HaKDMoDz
string s2 = s1.Replace(",", ",\r\n");
string s2 = s1.Replace(",", ",\r\n");
回答by Eric L
I propose use StringBuilder
我建议使用 StringBuilder
string s1 = "'99024','99050','99070','99143','99173','99191','99201','99202','99203','99204','99211','99212','99213','99214','99215','99217','99218','99219','99221','99222','99231','99232','99238','99239','99356','99357','99371','99374','99381','99382','99383','99384','99385','99386','99391','99392'";
var stringBuilder = new StringBuilder();
foreach (var s in s1.Split(','))
{
stringBuilder.Append(s).Append(",").AppendLine();
}
Console.WriteLine(stringBuilder);

