如何将逗号分隔的字符串转换为数组? [英] How can I convert a comma-separated string to an array?
本文介绍了如何将逗号分隔的字符串转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个逗号分隔的字符串,我想将其转换为数组,以便可以循环遍历它.
I have a comma-separated string that I want to convert into an array, so I can loop through it.
有内置的功能吗?
例如,我有这个字符串
var str = "January,February,March,April,May,June,July,August,September,October,November,December";
现在我想用逗号将其分割,然后将其存储在数组中.
Now I want to split this by the comma, and then store it in an array.
推荐答案
var array = string.split(',');
MDN参考,主要是对于limit
参数的可能意外行为很有帮助. (提示:"a,b,c".split(",", 2)
出现在["a", "b"]
上,而不是["a", "b,c"]
.)
MDN reference, mostly helpful for the possibly unexpected behavior of the limit
parameter. (Hint: "a,b,c".split(",", 2)
comes out to ["a", "b"]
, not ["a", "b,c"]
.)
这篇关于如何将逗号分隔的字符串转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文