Javascript separate string into different variables

后端 未结 5 1147
清歌不尽
清歌不尽 2020-12-11 13:12

I am looking to take a string and find all the spaces in it and separate that into different variables. I know I could use the .split() but that wouldn\'t make

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 13:35

    If you don't know how many spaces there are in the string, beforehand, you can do the following:

    var str = "How are you doing today, my dear friend?";
    numberspaces = str.split(" ").length; //you know how many spaces there are
    words=str.split(" ");                 //it creates an array of words
    var i;
    for (i = 0; i < numberspaces; i++){
    console.log(words[i]);}
    

提交回复
热议问题