我有一个逗号分隔的字符串,我想把它转换成一个数组,这样我就可以遍历它。
有什么内置的功能吗?
例如,我有这个字符串
var str = "January,February,March,April,May,June,July,August,September,October,November,December";
现在我想用逗号将其拆分,然后将其存储在数组中。
我有一个逗号分隔的字符串,我想把它转换成一个数组,这样我就可以遍历它。
有什么内置的功能吗?
例如,我有这个字符串
var str = "January,February,March,April,May,June,July,August,September,October,November,December";
现在我想用逗号将其拆分,然后将其存储在数组中。
当前回答
请注意:
var a = "";
var x = new Array();
x = a.split(",");
alert(x.length);
将提醒1
其他回答
嗯,拆分是危险的,因为字符串总是可以包含逗号。注意以下事项:
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);
如果你的目标是整数,比如1,2,3,4,5,请注意。如果要在拆分字符串后将数组元素用作整数而不是字符串,请考虑将它们转换为整数。
var str = "1,2,3,4,5,6";
var temp = new Array();
// This will return an array with strings "1", "2", etc.
temp = str.split(",");
添加这样的循环,
for (a in temp ) {
temp[a] = parseInt(temp[a], 10); // Explicitly include base as per Álvaro's comment
}
将返回一个包含整数而非字符串的数组。
下面是一个函数,它将字符串转换为数组,即使列表中只有一项(没有分隔符):
function listToAray(fullString, separator) {
var fullArray = [];
if (fullString !== undefined) {
if (fullString.indexOf(separator) == -1) {
fullArray.push(fullString);
} else {
fullArray = fullString.split(separator);
}
}
return fullArray;
}
这样使用:
var myString = 'alpha,bravo,charlie,delta';
var myArray = listToArray(myString, ',');
myArray[2]; // charlie
var yourString = 'echo';
var yourArray = listToArray(yourString, ',');
yourArray[0]; // echo
我创建这个函数是因为如果字符串中没有分隔符(只有一个项目),split会抛出一个错误。
返回函数
var array = (new Function("return [" + str+ "];")());
其接受字符串和对象字符串:
var string = "0,1";
var objectstring = '{Name:"Tshirt", CatGroupName:"Clothes", Gender:"male-female"}, {Name:"Dress", CatGroupName:"Clothes", Gender:"female"}, {Name:"Belt", CatGroupName:"Leather", Gender:"child"}';
var stringArray = (new Function("return [" + string+ "];")());
var objectStringArray = (new Function("return [" + objectstring+ "];")());
JSFiddle公司https://jsfiddle.net/7ne9L4Lj/1/
对于逗号分隔字符串的字符串数组:
let months = ["January","Feb"];
let monthsString = months.join(", ");