admin管理员组文章数量:1134246
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]);
}
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]);
}
Share
Improve this question
edited Sep 28, 2021 at 5:09
Super Kai - Kazuya Ito
1
asked Apr 22, 2011 at 10:55
BrunoBruno
9,00713 gold badges39 silver badges55 bronze badges
1
|
9 Answers
Reset to default 197let text = "";
for(let member in list) {
text += list[member];
}
You can also keep adding strings to an existing string like so:
var myString = "Hello ";
myString += "World";
myString += "!";
the result would be -> Hello World!
simply used the +
operator. Javascript concats strings with +
It sounds like you want to use join
, e.g.:
var text = list.join();
To use String.concat, you need to replace your existing text, since the function does not act by reference.
let text = "";
for (const member in list) {
text = text.concat(list[member]);
}
Of course, the join() or += suggestions offered by others will work fine as well.
Simple use text = text + string2
Try this. It adds the same char multiple times to a string
const addCharsToString = (string, char, howManyTimes) => {
string + new Array(howManyTimes).fill(char).join('')
}
You can also use string interpolation
let text = "";
for(let member in list) {
text = `${text}${list[member]}`;
}
your array of strings (list) could work with map and join; (Also possible to additionally change strings if you want)
var text = list.map(i => `${i}`).join(' ')
would return First Name
but if you want to add more things around the name, above template also helps:
var text = list.map(i => `'${i}'`).join(' ')
would return 'First' 'Name'
and in case you would want to have a sequence of names, separeted by comma
var text = list.map(i => `'${i}'`).join(',')
would return 'First','Name','Second','Third',...
本文标签: Add characters to a string in JavascriptStack Overflow
版权声明:本文标题:Add characters to a string in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736776385a1952363.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
for...in
but a normalfor
loop. More information here: developer.mozilla.org/en/JavaScript/Reference/Statements/… Btw. if you look closely at both of your examples, you can see the difference (name=first_name.concat(last_name)
vstext.concat(list[member])
– Felix Kling Commented Apr 22, 2011 at 11:16