将字符串拆分为数组 [英] Split string into array

查看:85
本文介绍了将字符串拆分为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 JS 中,如果您想将用户条目拆分为一个数组,那么最好的方法是什么?

In JS if you would like to split user entry into an array what is the best way of going about it?

例如:

entry = prompt("Enter your name")

for (i=0; i<entry.length; i++)
{
entryArray[i] = entry.charAt([i]);
}

// entryArray=['j', 'e', 'a', 'n', 's', 'y'] after loop

也许我的做法是错误的 - 非常感谢您的帮助!

Perhaps I'm going about this the wrong way - would appreciate any help!

推荐答案

使用.split() 方法.当指定一个空字符串作为分隔符时,split() 方法将返回一个数组,每个字符一个元素.

Use the .split() method. When specifying an empty string as the separator, the split() method will return an array with one element per character.

entry = prompt("Enter your name")
entryArray = entry.split("");

这篇关于将字符串拆分为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆