Array to a string of switch array join (), split ();

join() 

join () method for all elements in the array into a string.

Elements are separated by a specified delimiter. arrayObject.join ( Separator ), using default comma separated

var arr = ['a','b','c','d','e','f'];
arr.join() // a,b,c,d,e,f
arr.join("-") // a-b-c-d-e-f

split()

split () method for dividing a character string into a string array. stringObject.split (separator, howmany)

 separator: Required. String or a regular expression, from the split stringObject parameter designated place.

howmany: Optional. This parameter specifies the maximum length of the array returned. If this parameter is set, returns the substring than this parameter is not specified array.

If this parameter is not set, the entire string will be split, without regard to its length.

var str="Do you want to have a holiday?"
str.split(" ")  //  Do,you,want,to,have,a,holiday?
str.split(/\s+/)  //  Do,you,want,to,have,a,holiday? 正则表达式分割
str.split("") // D,o, ,y,o,u, ,w,a,n,t, ,t,o, ,h,a,v,e, ,a, ,h,o,l,i,d,a,y,?
str.split(" ",3) //  Do,you,want  3表示返回长度为3的数组

var str="Do-you-want-to-have-a-holiday?"
str.split("-") //  Do,you,want,to,have,a,holiday?

 

Guess you like

Origin www.cnblogs.com/lvsk/p/11995740.html