String.ToCharArray() 等价于 JavaScript?

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

String.ToCharArray() equivalent on JavaScript?

javascript

提问by ajax333221

I am trying to find a way to split a string for every character on JavaScript, an equivalent to String.ToCharArray()from c#

我试图找到一种方法来为 JavaScript 上的每个字符拆分一个字符串,相当于String.ToCharArray()来自 c#

To later join them with commas.

稍后用逗号连接它们。

ex: "012345"after splitting -> "['0','1','2','3','4','5']"after join -> "0,1,2,3,4,5"

例如:"012345"拆分后 ->"['0','1','2','3','4','5']"加入后 ->"0,1,2,3,4,5"

So far what I have come across is to loop on every character and manually add the commas (I think this is very slow)

到目前为止,我遇到的是循环每个字符并手动添加逗号(我认为这很慢)

回答by BoltClock

This is a much simpler way to do it:

这是一种更简单的方法:

"012345".split('').join(',')

The same thing, except with comments:

同样的事情,除了评论:

"012345".split('') // Splits into chars, returning ["0", "1", "2", "3", "4", "5"]
        .join(',') // Joins each char with a comma, returning "0,1,2,3,4,5"

Notice that I pass an empty string to split(). If you don't pass anything, you'll get an array containing only the original string, rather than an array containing each character.

请注意,我将一个空字符串传递给split(). 如果你不传递任何东西,你将得到一个只包含原始字符串的数组,而不是一个包含每个字符的数组。

Alternatively you could pass nothing to join()and it'd use a comma by default, but in cases like this I prefer to be specific.

或者,您可以不传递任何内容join(),默认情况下它会使用逗号,但在这种情况下,我更愿意具体说明。

Don't worry about speed — I'm sure there isn't any appreciable difference. If you're so concerned, there isn't anything wrong with a loop either, though it might be more verbose.

不要担心速度 - 我确定没有任何明显的差异。如果您如此担心,循环也没有任何问题,尽管它可能更冗长。

回答by Edgardo Rodriguez

Maybe you could use the "Destructuring" feature:

也许您可以使用“解构”功能:

let str = "12345";
//convertion to array:
let strArr = [...str]; // strArr = ["1", "2", "3", "4", "5"]

回答by Sam Regnier

  1. This is a function to make a single word into a char array. Not full proof but does not take much to make it.

    function toCharArray(str){
         charArray =[];
         for(var i=0;i<str.length;i++){
              charArray.push(str[i]);
         }
    
         return charArray;
    }
    
  1. 这是一个将单个单词转换为字符数组的函数。不是完整的证据,但不需要太多。

    function toCharArray(str){
         charArray =[];
         for(var i=0;i<str.length;i++){
              charArray.push(str[i]);
         }
    
         return charArray;
    }
    

回答by Sebastien

Using Array.from is probably more explicit.

使用 Array.from 可能更明确。

Array.from("012345").join(',') // returns "0,1,2,3,4,5"

Array.from

数组.from

回答by Mikeumus

You may use Array's prototype map method called on a string:

您可以使用在字符串上调用的 Array 原型映射方法:

Array.prototype.map.call('012345', i => i); 
// ["0", "1", "2", "3", "4", "5"]

See "Using map generically" section of the MDN's Array.prototype.maparticle here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

请参阅Array.prototype.map此处MDN文章的“通用地图”部分:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map