Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

javascript - 如何将逗号分隔的字符串转换为数组?(How to 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.

(我有一个逗号分隔的字符串,我想将其转换成数组,因此可以循环遍历它。)

Is there anything built-in to do this?

(有内置的功能吗?)

For eg I have this string

(例如,我有这个字符串)

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.

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

  ask by Blankman translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
var array = string.split(',');

MDN reference , mostly helpful for the possibly unexpected behavior of the limit parameter.

(MDN参考 ,主要对limit参数可能发生的异常行为很有帮助。)

(Hint: "a,b,c".split(",", 2) comes out to ["a", "b"] , not ["a", "b,c"] .)

((提示: "a,b,c".split(",", 2)出现在["a", "b"] ,而不是["a", "b,c"] 。))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...