Convert string with commas to array

后端 未结 18 1144
执念已碎
执念已碎 2020-11-22 12:08

How can I convert a string to a JavaScript array?

Look at the code:

var string = \"0,1\";
var array = [string];
alert(array[0]);

In

18条回答
  •  醉话见心
    2020-11-22 12:24

    How to Convert Comma Separated String into an Array in JavaScript?

    var string = 'hello, world, test, test2, rummy, words';
    var arr = string.split(', '); // split string on comma space
    console.log( arr );
     
    //Output
    ["hello", "world", "test", "test2", "rummy", "words"]
    

    For More Examples of convert string to array in javascript using the below ways:

    1. Split() – No Separator:

    2. Split() – Empty String Separator:

    3. Split() – Separator at Beginning/End:

    4. Regular Expression Separator:

    5. Capturing Parentheses:

    6. Split() with Limit Argument

      check out this link ==> https://www.tutsmake.com/javascript-convert-string-to-array-javascript/

提交回复
热议问题