javascript 用逗号将字符串拆分为新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15375886/
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
Split string with commas to new line
提问by Andy
I have a string like
我有一个像
This is great day, tomorrow is a better day, the day after is a better day, the day after the day after that is the greatest day
I wanted to basically split this one long string at the commas and insert a new line so it becomes
我想基本上在逗号处拆分这个长字符串并插入一个新行,这样它就变成了
This is great day
tomorrow is a better day
the day after is a better day
the day after the day after that is the greatest day
How can I do that ?
我怎样才能做到这一点 ?
回答by Benjamin Gruenbaum
With the built in splitand joinmethods
var formattedString = yourString.split(",").join("\n")
If you'd like the newlines to be HTML line breaks that would be
如果您希望换行符是 HTML 换行符,那就是
var formattedString = yourString.split(",").join("<br />")
This makes the most sense to me since you're splitting it into lines and then joining them with the newline character.
这对我来说最有意义,因为您将它分成几行,然后用换行符将它们连接起来。
Although I think speed is less important than readability in most cases, I was curious about it in this case so I've written a quick a benchmark.
虽然我认为在大多数情况下速度不如可读性重要,但在这种情况下我很好奇所以我写了一个快速的基准测试。
It seems that (in chrome) using str.split(",").join("\n")
is faster than str.replace(/,/g, '\n');
.
似乎(在 chrome 中)使用str.split(",").join("\n")
比str.replace(/,/g, '\n');
.
回答by Blender
You could also replace them:
你也可以替换它们:
string.replace(/,/g, '\n');
回答by MADHUR GUPTA
<p id="errorMessage">Click | the |button |to |display| the| array| values|
after| the| split.</p>
$(document).ready(function () {
var str = $("#errorMessage").text();
document.getElementById("errorMessage").innerHTML=str.split("|").join("
</br>"); }
回答by imkost
> a = 'This is great day, tomorrow is a better day, the day after is a better day, the day after the day after that is the greatest day'
> b = a.split(', ').join('\n')
"This is great day
tomorrow is a better day
the day after is a better day
the day after the day after that is the greatest day"
回答by Hristo
You can use .split()
to create an array of all the parts of the string...
您可以使用.split()
创建字符串所有部分的数组...
var str = 'This is great day, tomorrow is a better day, the day after is a better day, the day after the day after that is the greatest day';
str.split(',');
-> ["This is great day", " tomorrow is a better day", " the day after is a better day", " the day after the day after that is the greatest day"]
Now, you can do whatever you want with the different parts. Since you want to join with a new line, you can use .join()
to put it back together...
现在,您可以对不同的部分做任何想做的事情。既然你想加入一个新行,你可以用.join()
把它重新组合起来......
str.split(',').join('\n');
-> "This is great day
tomorrow is a better day
the day after is a better day
the day after the day after that is the greatest day"
回答by AkisC
Without testing on my browser try:
无需在我的浏览器上进行测试,请尝试:
var MyStr="This is great day, tomorrow is a better day, the day after is a better day, the day after the day after that is the greatest day";
Var splitedStr = MyStr.split(",");
var returnStr = '';
for (var i = 0; i < splitedStr.length; i++)
{
returnStr += splitedStr[i] + '<br />';
}
document.write(returnStr);