【问题标题】:Split string into array [duplicate]将字符串拆分为数组[重复]
【发布时间】:2011-12-20 06:03:36
【问题描述】:

在 JS 中,如果您想将用户条目拆分为一个数组,那么最好的方法是什么?

例如:

entry = prompt("Enter your name")

for (i=0; i<entry.length; i++)
{
entryArray[i] = entry.charAt([i]);
}

// entryArray=['j', 'e', 'a', 'n', 's', 'y'] after loop

也许我做错了 - 非常感谢任何帮助!

【问题讨论】:

    标签: javascript arrays string split


    【解决方案1】:

    使用.split() 方法。当指定一个空字符串作为分隔符时,split() 方法将返回一个每个字符一个元素的数组。

    entry = prompt("Enter your name")
    entryArray = entry.split("");
    

    【讨论】:

    【解决方案2】:

    ES6:

    const array = [...entry]; // entry="i am" => array=["i"," ","a","m"]
    

    【讨论】:

      【解决方案3】:

      使用var array = entry.split("");

      【讨论】:

        【解决方案4】:

        你关心非英文名字吗?如果是这样,所有提出的解决方案(.split('')、[...str]、Array.from(str) 等)可能会产生不好的结果,具体取决于语言:

        "प्रणव मुखर्जी".split("") // the current president of India, Pranab Mukherjee
        // returns ["प", "्", "र", "ण", "व", " ", "म", "ु", "ख", "र", "्", "ज", "ी"]
        // but should return ["प्", "र", "ण", "व", " ", "मु", "ख", "र्", "जी"]
        

        考虑使用 grapheme-splitter 库进行干净的基于标准的拆分: https://github.com/orling/grapheme-splitter

        【讨论】:

          【解决方案5】:
          var foo = 'somestring'; 
          

          // bad example https://stackoverflow.com/questions/6484670/how-do-i-split-a-string-into-an-array-of-characters/38901550#38901550
          
          var arr = foo.split(''); 
          console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]
          

          // good example
          var arr = Array.from(foo);
          console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]
          

          // best
          var arr = [...foo]
          console.log(arr); // ["s", "o", "m", "e", "s", "t", "r", "i", "n", "g"]
          

          【讨论】:

            【解决方案6】:

            你可以试试这个:

            var entryArray = Array.prototype.slice.call(entry)

            【解决方案7】:

            ...也适合那些喜欢 CS 文学的人。

            array = Array.from(entry);
            

            【讨论】:

              【解决方案8】:

              使用split方法:

              entry = prompt("Enter your name");
              entryArray = entry.split("");
              

              更多信息请参考String.prototype.split()

              【讨论】:

                【解决方案9】:

                ES6 在遍历对象(字符串、数组、映射、集合)方面非常强大。 让我们使用扩展运算符来解决这个问题。

                entry = prompt("Enter your name");
                var count = [...entry];
                console.log(count);
                

                【讨论】:

                  【解决方案10】:

                  你可以这样试试:

                  let entry = prompt("Enter your name") 
                  let entryArray = entry.split('')
                  console.log(entryArray)
                  

                  这里是小提琴 https://jsfiddle.net/swapanil/Lp1arvqc/17/

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2013-08-19
                    • 1970-01-01
                    • 2013-01-03
                    • 2016-01-15
                    • 2021-10-09
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-08-28
                    相关资源
                    最近更新 更多