Javascript 将字符串拆分为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7979727/
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 into array
提问by methuselah
In JS if you would like to split user entry into an array what is the best way of going about it?
在 JS 中,如果您想将用户条目拆分为一个数组,那么最好的方法是什么?
For example:
例如:
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!
也许我以错误的方式解决这个问题-希望得到任何帮助!
回答by James Hill
回答by Abdennour TOUMI
ES6 :
ES6:
const array = [...entry]; // entry="i am" => array=["i"," ","a","m"]
回答by Jordan Wallwork
use var array = entry.split("");
用 var array = entry.split("");
回答by Orlin Georgiev
Do you care for non-English names? If so, all of the presented solutions (.split(''), [...str], Array.from(str), etc.) may give bad results, depending on language:
你关心非英文名字吗?如果是这样,所有提供的解决方案(.split('')、[...str]、Array.from(str) 等)可能会产生不好的结果,具体取决于语言:
"????? ???????".split("") // the current president of India, Pranab Mukherjee
// returns ["?", "?", "?", "?", "?", " ", "?", "?", "?", "?", "?", "?", "?"]
// but should return ["??", "?", "?", "?", " ", "??", "?", "??", "??"]
Consider using the grapheme-splitter library for a clean standards-based split: https://github.com/orling/grapheme-splitter
考虑使用 grapheme-splitter 库进行干净的基于标准的拆分:https: //github.com/orling/grapheme-splitter
回答by Mic
You can try this:
你可以试试这个:
var entryArray = Array.prototype.slice.call(entry)
var entryArray = Array.prototype.slice.call(entry)
回答by Redu
...and also for those who like literature in CS.
...以及那些喜欢 CS 文学的人。
array = Array.from(entry);
回答by Lukman
Use split
method:
使用split
方法:
entry = prompt("Enter your name");
entryArray = entry.split("");
Refer String.prototype.split()
for more info.
String.prototype.split()
有关更多信息,请参阅。
回答by аlex dyky?
var foo = 'somestring';
// bad example https://stackoverflow.com/questions/6484670/how-do-i-split-a-string-into-an-array-of-characters/38901550#38901550
var arr = foo.split('');
console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]
// good example
var arr = Array.from(foo);
console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]
// best
var arr = [...foo]
console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]
回答by JideLambo
ES6 is quite powerful in iterating through objects (strings, Array, Map, Set). Let's use a Spread Operator to solve this.
ES6 在遍历对象(字符串、数组、映射、集合)方面非常强大。让我们使用扩展运算符来解决这个问题。
entry = prompt("Enter your name");
var count = [...entry];
console.log(count);
回答by JideLambo
You can try this way:
你可以试试这个方法:
let entry = prompt("Enter your name")
let entryArray = entry.split('')
console.log(entryArray)
here is fiddle https://jsfiddle.net/swapanil/Lp1arvqc/17/