创建一个包含特定字符串的数组,以及如何将字符串分成字母[重复]

时间:2023-02-14 18:13:46

This question already has an answer here:

这个问题在这里已有答案:

I want to create an array and add a string to the array at zero index. I want to divide the string into separate letters (suppose I have the string java; I want to convert it into j,a,v,a). Is there any predefined methods for arrays in JavaScript?

我想创建一个数组并在零索引处向数组添加一个字符串。我想将字符串分成单独的字母(假设我有字符串java;我想将其转换为j,a,v,a)。 JavaScript中是否有任何预定义的数组方法?

4 个解决方案

#1


2  

Splitting a string is as easy as :

拆分字符串非常简单:

"hello".split("")

In order to insert an array into a array at a certain index, you have to use splice. For example:

为了将数组插入某个索引的数组中,必须使用splice。例如:

var array = [1,2,3,4],
    hello = "hello".split("");

array.splice.apply(array, [0, 0].concat(hello));

The last line isn't that easy to understand at first. In javascript you can use apply on any function to call a function with parameters as array.

最后一行起初并不容易理解。在javascript中,您可以在任何函数上使用apply来调用参数为array的函数。

All it's doing is taking 0 element at index 0 in array and inserting the array hello at this position. You should read more about split and splice.

它所做的只是在数组中的索引0处取0元素并在此位置插入数组hello。您应该阅读有关拆分和拼接的更多信息。

Splitting on an empty string will split on any character. But you can pass regex to split etc. So it's quite powerful.

拆分空字符串将拆分任何字符。但你可以通过正则表达式分裂等等。所以它非常强大。

#2


0  

Try This:

 var array = string.split(''); // where sting is your string

#3


0  

Use this:

var str = 'java';
str.split(''); //["j", "a", "v", "a"]

#4


0  

Use split() and join() to achieve your requirement.

使用split()和join()来满足您的要求。

 var xArray=[];
 //Inserting a string at 0th index of an array
 xArray[xArray.length] = "JAVA";
 //Splitting that String with empty and joining the returned array with ","
 xArray[xArray.length - 1] =  xArray[xArray.length - 1].split("").join(",")

 alert(xArray[xArray.length - 1]); //J,A,V,A

#1


2  

Splitting a string is as easy as :

拆分字符串非常简单:

"hello".split("")

In order to insert an array into a array at a certain index, you have to use splice. For example:

为了将数组插入某个索引的数组中,必须使用splice。例如:

var array = [1,2,3,4],
    hello = "hello".split("");

array.splice.apply(array, [0, 0].concat(hello));

The last line isn't that easy to understand at first. In javascript you can use apply on any function to call a function with parameters as array.

最后一行起初并不容易理解。在javascript中,您可以在任何函数上使用apply来调用参数为array的函数。

All it's doing is taking 0 element at index 0 in array and inserting the array hello at this position. You should read more about split and splice.

它所做的只是在数组中的索引0处取0元素并在此位置插入数组hello。您应该阅读有关拆分和拼接的更多信息。

Splitting on an empty string will split on any character. But you can pass regex to split etc. So it's quite powerful.

拆分空字符串将拆分任何字符。但你可以通过正则表达式分裂等等。所以它非常强大。

#2


0  

Try This:

 var array = string.split(''); // where sting is your string

#3


0  

Use this:

var str = 'java';
str.split(''); //["j", "a", "v", "a"]

#4


0  

Use split() and join() to achieve your requirement.

使用split()和join()来满足您的要求。

 var xArray=[];
 //Inserting a string at 0th index of an array
 xArray[xArray.length] = "JAVA";
 //Splitting that String with empty and joining the returned array with ","
 xArray[xArray.length - 1] =  xArray[xArray.length - 1].split("").join(",")

 alert(xArray[xArray.length - 1]); //J,A,V,A