array conversion string

One: array to string (3 methods)
toString(), toLocaleString(), join(), join(',')

JS allows conversion between arrays and strings. Among them, the Array method object defines 3 methods, as follows

Array method description
toString() converts an array into a string toLocaleString() converts an array into a
string of local conventions join () joins elements of an array to build a string Elements are placed into a string Elements are separated by the specified delimiter


Delimiter specified by join() Description
join() can be interpreted as a string directly, and the default comma separates
join(' ') empty connection
join(',') or join('-') or join('.') The comma in the middle is added manually, and it can also be changed to something else such as,. ! - etc.
// join()
let a= ["00", "01", "02", "03", "04"]
let b= a.join()      
console.log(b)
/ / 00,01,02,03,04
console. log( typeof b)
// string

或者
// join('')
let a= ["00", "01", "02", "03", "04"]
let b=  a.join('')      
console.log(b)
//  0001020304
console.log( typeof b)
// string

或者
// join(',')
let a= ["00", "01", "02", "03", "04"]
let b=  a.join(',')      
console.log(b)
//  00,01,02,03,04

或者
// join('-')
let a= ["00", "01", "02", "03", "04"]
let b=  a.join('-')      
console.log(b)
//  00-01-02-03-04

或者
// join('!')
let a= ["00", "01", "02", "03", "04"]
let b=  a.join('!')      
console.log(b)
// 00!01!02!03!04


2. The toString() method can convert a logical value into a string and return the result
let a= ["00", "01", "02", "03", "04"]
let c = a.toString( ); //Convert the array to a string
console.log(c)
// 00,01,02,03,04
console.log(typeof c); //Return string string, indicating that it is a string type

toString() The method cannot specify the delimiter, but we can specify the replacement through the replace() method

let a= ["00", "01", "02", "03", "04"]
let f = a.toString().replace(/,/gi,'-')
console.log(f)
//Print result: 00-01-02-03-04

3. toLocaleString()
converts the array into a locally agreed string

let a= ["00", "01", "02", "03", "04"]
let e = a.toLocaleString();  
console.log(e) 
//Print results: 00,01,02, 03,04

2: Convert string to array (2 methods)
String method Description
split() method converts a string into an array
Spread operator (…) The spread operator
1 in es6, the split() method is used to convert Splitting a string into a string array
is also used to split a string into a string array, what is the difference between split(','), split(), and split(' ')?

split() method description
split(',') Each character is separated by a comma
split() can be understood as a string directly, the default comma is separated
split(' ') Empty string Each character will be split
let arr = 'aa,bb,cc,dd'
let newStr = arr.split(',')
console.log(newStr) 
// ["aa", "bb", "cc", "dd"]

let arr = 'aa,bb,cc,dd'
let newStr = arr.split()
console.log(newStr)
// ["aa,bb,cc,dd"]

if an empty string ("") is used as separator, then each character in stringObject will be divided

let arr = 'aa,bb,cc,dd'
let newStr = arr.split('')
console.log(newStr) 
// ["a", "a", ",", "b", "b" , ",", "c", "c", ",", "d", "d"]

2. The expansion operator in es6
let arr = 'aa,bb,cc,dd'
let newStr = [. ..arr]
console.log(newStr) 
// ["a", "a", ",", "b", "b", ",", "c", "c", ",", " d", "d"]

Guess you like

Origin blog.csdn.net/qq_43532275/article/details/130859762