在 Javascript 中向字符串添加字符
我需要将 For 循环 字符添加到空字符串中。我知道你可以使用 Javascript 中的函数 concat 来对字符串进行连接,
var first_name = "peter";
var last_name = "jones";
var name=first_name.concat(last_name)
但它不适用于我的示例。 知道如何以另一种方式做到这一点吗?
我的代码:
var text ="";
for (var member in list) {
text.concat(list[member]);
}
I need to add in a For Loop characters to an empty string. I know that you can use the function concat in Javascript to do concats with strings
var first_name = "peter";
var last_name = "jones";
var name=first_name.concat(last_name)
But it doesn't work with my example.
Any idea of how to do it in another way?
My code :
var text ="";
for (var member in list) {
text.concat(list[member]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您还可以继续向现有字符串添加字符串,如下所示:
结果将是 ->
世界你好!
You can also keep adding strings to an existing string like so:
the result would be ->
Hello World!
只需使用
+
运算符即可。 Javascript 使用 + 连接字符串simply used the
+
operator. Javascript concats strings with +听起来您想使用
join
,例如:It sounds like you want to use
join
, e.g.:要使用 String.concat,您需要替换现有文本,因为该函数不按引用运行。
当然,其他人提供的 join() 或 += 建议也可以正常工作。
To use String.concat, you need to replace your existing text, since the function does not act by reference.
Of course, the join() or += suggestions offered by others will work fine as well.
使用简单
文本 = 文本 + 字符串2
Simple use
text = text + string2
试试这个。它将相同的字符多次添加到字符串中
Try this. It adds the same char multiple times to a string
您还可以使用字符串插值
You can also use string interpolation
您的字符串数组(列表)可以与映射和连接一起使用; (如果需要,也可以另外更改字符串)
将返回
名字
但如果您想在名称周围添加更多内容,上面的模板也有帮助:
将返回
'First' 'Name'
如果您想要一系列名称,以逗号分隔,
将返回
'First','Name','Second','Third' ,...
your array of strings (list) could work with map and join; (Also possible to additionally change strings if you want)
would return
First Name
but if you want to add more things around the name, above template also helps:
would return
'First' 'Name'
and in case you would want to have a sequence of names, separeted by comma
would return
'First','Name','Second','Third',...