我有一个逗号分隔的字符串,我想把它转换成一个数组,这样我就可以遍历它。

有什么内置的功能吗?

例如,我有这个字符串

var str = "January,February,March,April,May,June,July,August,September,October,November,December";

现在我想用逗号将其拆分,然后将其存储在数组中。


当前回答

嗯,拆分是危险的,因为字符串总是可以包含逗号。注意以下事项:

var myArr = "a,b,c,d,e,f,g,','";
result = myArr.split(',');

那么你会怎么解释呢?你希望结果是什么?具有以下内容的数组:

['a', 'b', 'c', 'd', 'e', 'f', 'g', '\'', '\''] or
['a', 'b', 'c', 'd', 'e', 'f', 'g', ',']

即使你逃避逗号,你也会有问题。

我很快就把这件事弄混了:

(function($) {
    $.extend({
        splitAttrString: function(theStr) {
            var attrs = [];

            var RefString = function(s) {
                this.value = s;
            };
            RefString.prototype.toString = function() {
                return this.value;
            };
            RefString.prototype.charAt = String.prototype.charAt;
            var data = new RefString(theStr);

            var getBlock = function(endChr, restString) {
                var block = '';
                var currChr = '';
                while ((currChr != endChr) && (restString.value !== '')) {
                    if (/'|"/.test(currChr)) {
                        block = $.trim(block) + getBlock(currChr, restString);
                    }
                    else if (/\{/.test(currChr)) {
                        block = $.trim(block) + getBlock('}', restString);
                    }
                    else if (/\[/.test(currChr)) {
                        block = $.trim(block) + getBlock(']', restString);
                    }
                    else {
                        block += currChr;
                    }
                    currChr = restString.charAt(0);
                    restString.value = restString.value.slice(1);
                }
                return $.trim(block);
            };

            do {
                var attr = getBlock(',', data);
                attrs.push(attr);
            }
            while (data.value !== '')
                ;
            return attrs;
        }
    });
})(jQuery);

其他回答

let str = "January,February,March,April,May,June,July,August,September,October,November,December"

let arr = str.split(',');

这将导致:

["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

如果要将以下内容转换为:

["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]

这:

"January,February,March,April,May,June,July,August,September,October,November,December";

use:

str = arr.join(',')

将逗号分隔的字符串传递到此函数,它将返回一个数组,如果找不到逗号分隔字符串,则返回null。

function splitTheString(CommaSepStr) {
    var ResultArray = null;

    // Check if the string is null or so.
    if (CommaSepStr!= null) {

        var SplitChars = ',';

        // Check if the string has comma of not will go to else
        if (CommaSepStr.indexOf(SplitChars) >= 0) {
            ResultArray = CommaSepStr.split(SplitChars);

        }
        else {

            // The string has only one value, and we can also check
            // the length of the string or time and cross-check too.
            ResultArray = [CommaSepStr];
        }
    }
    return ResultArray;
}
var array = string.split(',');

MDN引用,对于极限参数的可能意外行为非常有用。(提示:“a,b,c”.split(“,”,2)指向[“a”,“b”],而不是[“a,”b,c“]。)

如果用户通过添加额外的空格键入错误。你可以用这样的东西。

tags: foo,  zar, gar
const stringToArr = (string) => {
  return string.trim.split(",");
};
let myString = "January,February,March,April,May,June,July,August,September,October,November,December";
      const temp=myString .split(",");
      console.log(temp);

输出:-[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”]

非常简单,您可以使用拆分默认javascript函数。