在 Javascript 中向字符串添加字符

发布于 2024-11-02 13:51:20 字数 353 浏览 1 评论 0原文

我需要将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

久夏青 2024-11-09 13:51:20
let text = "";
for(let member in list) {
  text += list[member];
}
let text = "";
for(let member in list) {
  text += list[member];
}
你的笑 2024-11-09 13:51:20

您还可以继续向现有字符串添加字符串,如下所示:

var myString = "Hello ";
myString += "World";
myString += "!";

结果将是 -> 世界你好!

You can also keep adding strings to an existing string like so:

var myString = "Hello ";
myString += "World";
myString += "!";

the result would be -> Hello World!

乖乖兔^ω^ 2024-11-09 13:51:20

只需使用 + 运算符即可。 Javascript 使用 + 连接字符串

simply used the + operator. Javascript concats strings with +

澜川若宁 2024-11-09 13:51:20

听起来您想使用 join,例如:

var text = list.join();

It sounds like you want to use join, e.g.:

var text = list.join();
御守 2024-11-09 13:51:20

要使用 String.concat,您需要替换现有文本,因为该函数不按引用运行。

let text = "";
for (const member in list) {
  text = text.concat(list[member]);
}

当然,其他人提供的 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.

所谓喜欢 2024-11-09 13:51:20

使用简单
文本 = 文本 + 字符串2

Simple use
text = text + string2

怪我太投入 2024-11-09 13:51:20

试试这个。它将相同的字符多次添加到字符串中

const addCharsToString = (string, char, howManyTimes) => {
  string + new Array(howManyTimes).fill(char).join('')
} 

Try this. It adds the same char multiple times to a string

const addCharsToString = (string, char, howManyTimes) => {
  string + new Array(howManyTimes).fill(char).join('')
} 
未央 2024-11-09 13:51:20

您还可以使用字符串插值

let text = "";
for(let member in list) {
  text = `${text}${list[member]}`;
}

You can also use string interpolation

let text = "";
for(let member in list) {
  text = `${text}${list[member]}`;
}
┈┾☆殇 2024-11-09 13:51:20

您的字符串数组(列表)可以与映射和连接一起使用; (如果需要,也可以另外更改字符串)

var text = list.map(i => `${i}`).join(' ') 

将返回 名字

但如果您想在名称周围添加更多内容,上面的模板也有帮助:

var text = list.map(i => `'${i}'`).join(' ') 

将返回 'First' 'Name'

如果您想要一系列名称,以逗号分隔,

var text = list.map(i => `'${i}'`).join(',') 

将返回 'First','Name','Second','Third' ,...

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',...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文