Javascript 如何将字符串拆分为字符数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6484670/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:58:51  来源:igfitidea点击:

How do I split a string into an array of characters?

javascriptstring

提问by DrStrangeLove

var s = "overpopulation";
var ar = [];
ar = s.split();
alert(ar);

I want to string.split a word into array of characters.

我想 string.split 一个单词成字符数组。

The above code doesn't seem to work - it returns "overpopulation" as Object..

上面的代码似乎不起作用 - 它返回“人口过剩”作为对象..

How do i split it into array of characters, if original string doesn't contain commas and whitespace?

如果原始字符串不包含逗号和空格,我如何将其拆分为字符数组?

回答by lonesomeday

You can split on an empty string:

您可以拆分空字符串:

var chars = "overpopulation".split('');


If you just want to access a string in an array-like fashion, you can do that without split:

如果您只想以类似数组的方式访问字符串,则可以不使用split

var s = "overpopulation";
for (var i = 0; i < s.length; i++) {
    console.log(s.charAt(i));
}


You can also access each character with its index using normal array syntax. Note, however, that strings are immutable, which means you can't setthe value of a character using this method, and that it isn't supported by IE7 (if that still matters to you).

您还可以使用普通数组语法访问每个字符及其索引。但是请注意,字符串是不可变的,这意味着您无法使用此方法设置字符的值,并且 IE7 不支持它(如果这对您仍然很重要)。

var s = "overpopulation";

console.log(s[3]); // logs 'r'

回答by Onur Y?ld?r?m

Old question but I should warn:

老问题,但我应该警告:

Do NOT use .split('')

不使用 .split('')

You'll get weird results with non-BMP(non-Basic-Multilingual-Plane) character sets.

使用非 BMP(非基本多语言平面)字符集会得到奇怪的结果。

Reason is that methods like .split()and .charCodeAt()only respect the characters with a code point below 65536; bec. higher code points are represented by a pairof (lower valued) "surrogate" pseudo-characters.

原因是方法喜欢.split()并且.charCodeAt()只尊重代码点低于 65536 的字符;贝克 较高的代码点由一对(较低值的)“代理”伪字符表示。

''.length     // —> 6
''.split('')  // —> ["?", "?", "?", "?", "?", "?"]

''.length      // —> 2
''.split('')   // —> ["?", "?"]

Use ES2015 (ES6) features where possible:

尽可能使用 ES2015 (ES6) 特性:

Using the spreadoperator:

使用扩展运算符:

let arr = [...str];

Or Array.from

或者 Array.from

let arr = Array.from(str);

Or splitwith the new uRegExp flag:

或者split使用新的uRegExp 标志:

let arr = str.split(/(?!$)/u);

Examples:

例子:

[...'']        // —> ["", "", ""]
[...'']     // —> ["", "", ""]

For ES5, options are limited:

对于 ES5,选项是有限的:

I came up with this function that internally uses MDN exampleto get the correct code point of each character.

我想出了这个函数,它在内部使用MDN 示例来获取每个字符的正确代码点。

function stringToArray() {
  var i = 0,
    arr = [],
    codePoint;
  while (!isNaN(codePoint = knownCharCodeAt(str, i))) {
    arr.push(String.fromCodePoint(codePoint));
    i++;
  }
  return arr;
}

This requires knownCharCodeAt()functionand for some browsers; a String.fromCodePoint()polyfill.

这需要knownCharCodeAt()功能和某些浏览器;一个String.fromCodePoint()polyfill。

if (!String.fromCodePoint) {
// ES6 Unicode Shims 0.1 , ? 2012 Steven Levithan , MIT License
    String.fromCodePoint = function fromCodePoint () {
        var chars = [], point, offset, units, i;
        for (i = 0; i < arguments.length; ++i) {
            point = arguments[i];
            offset = point - 0x10000;
            units = point > 0xFFFF ? [0xD800 + (offset >> 10), 0xDC00 + (offset & 0x3FF)] : [point];
            chars.push(String.fromCharCode.apply(null, units));
        }
        return chars.join("");
    }
}

Examples:

例子:

stringToArray('')     // —> ["", "", ""]
stringToArray('')  // —> ["", "", ""]


Note: str[index](ES5) and str.charAt(index)will also return weird results with non-BMP charsets. e.g. ''.charAt(0)returns "?".

注意str[index](ES5) 并且str.charAt(index)还会返回带有非 BMP 字符集的奇怪结果。例如''.charAt(0)返回"?"

UPDATE: Read this nice articleabout JS and unicode.

更新:阅读这篇关于 JS 和 unicode 的好文章

回答by pimvdb

It's as simple as:

这很简单:

s.split("");

The delimiter is an empty string, hence it will break up between each single character.

分隔符是一个空字符串,因此它会在每个单个字符之间分开。

回答by Orlin Georgiev

.split('') would split emojis in half.

.split('') 会将表情符号一分为二。

Onur's solutionsand the regex's proposed work for some emojis, but can't handle more complex languages or combined emojis. Consider this emoji being ruined:

Onur 的解决方案和正则表达式针对某些表情符号的建议工作,但无法处理更复杂的语言或组合表情符号。考虑一下这个表情符号被毁了:

[..."??"] // returns ["", "?", "?", ""]  instead of ["??"]

Also consider this Hindi text "????????" which is split like this:

还要考虑这个印地语文本“????????” 这是这样分割的:

[..."????????"]  // returns   ["?", "?", "?", "?", "?", "?", "?", "?"]

but should in fact be split like this:

但实际上应该像这样拆分:

["?","??","??","??","?"]

because some of the characters are combining marks (think diacritics/accents in European languages).

因为有些字符是组合标记(想想欧洲语言中的变音符号/重音符号)。

You can use the grapheme-splitter library for this:

您可以为此使用 grapheme-splitter 库:

https://github.com/orling/grapheme-splitter

https://github.com/orling/grapheme-splitter

It does proper standards-based letter split in all the hundreds of exotic edge-cases - yes, there are that many.

它在所有数百种奇特的边缘情况下进行了适当的基于标准的字母拆分 - 是的,有这么多。

回答by Diana Ionita

The split() method in javascript accepts two parameters: a separator and a limit. The separator specifies the character to use for splitting the string. If you don't specify a separator, the entire string is returned, non-separated. But, if you specify the empty string as a separator, the string is split between each character.

javascript 中的 split() 方法接受两个参数:分隔符和限制。分隔符指定用于拆分字符串的字符。如果不指定分隔符,则返回整个字符串,未分隔。但是,如果您指定空字符串作为分隔符,则字符串将在每个字符之间拆分。

Therefore:

所以:

s.split('')

will have the effect you seek.

会有你想要的效果。

More information here

更多信息在这里

回答by Jamie Dixon

A string in Javascript is already a character array.

Javascript 中的字符串已经是一个字符数组。

You can simply access any character in the array as you would any other array.

您可以像访问任何其他数组一样简单地访问数组中的任何字符。

var s = "overpopulation";
alert(s[0]) // alerts o.

UPDATE

更新

As is pointed out in the comments below, the above method for accessing a character in a string is part of ECMAScript 5 which certain browsers may not conform to.

正如下面的评论所指出的,上述访问字符串中字符的方法是 ECMAScript 5 的一部分,某些浏览器可能不符合。

An alternative method you can use is charAt(index).

您可以使用的另一种方法是charAt(index).

var s = "overpopulation";
    alert(s.charAt(0)) // alerts o.

回答by Gumbo

You can use the regular expression /(?!$)/:

您可以使用正则表达式/(?!$)/

"overpopulation".split(/(?!$)/)

The negative look-ahead assertion (?!$)will match right in front of every character.

否定前瞻断言(?!$)将匹配在每个字符的正前方。

回答by Murhaf Sousli

To support emojis use this

要支持表情符号,请使用此

('Dragon ').split(/(?!$)/u);

=> ['D', 'r', 'a', 'g', 'o', 'n', ' ', '']

=> ['D', 'r', 'a', 'g', 'o', 'n', ' ', '']