1: Array to string (3 methods)
It is also an array to string. What is the difference between toString(), toLocaleString(), join(), join (',')?
JavaScript allows the conversion between arrays and strings. The Array method object defines three methods that can convert an Array into a string, as shown in the table.
Array method | explain |
---|---|
toString() | Converts an array to a string |
toLocaleString() | Converts an array to a locally agreed string |
join() | Concatenate array elements to build a string |
1: The join() method is used to put all the elements in the array into a string
Elements are separated by a specified separator
Delimiter specified by join() | explain |
---|---|
join() | It can be understood as directly changing into a string, separated by commas by default |
join(' ') | Empty connection |
join(',') or join('-') or join('.) | The comma in the middle is added manually, or it can be changed to something else, such as- You can wait |
// join() var a= ["00", "01", "02", "03", "04"] var b= a.join() console.log(b) console.log( typeof b) //Print results 00,01,02,03,04
// join('') var a= ["00", "01", "02", "03", "04"] var b= a.join('') console.log(b) console.log( typeof b) //Print result 0001020304
// join(',') var a= ["00", "01", "02", "03", "04"] var b= a.join(',') console.log(b) console.log( typeof b) //Print results 00,01,02,03,04 perhaps // join('-') var a= ["00", "01", "02", "03", "04"] var b= a.join('-') console.log(b) console.log( typeof b) //Print result 00-01-02-03-04 perhaps // join('!') var a= ["00", "01", "02", "03", "04"] var b= a.join('!') console.log(b) console.log( typeof b) //Print result 00! 01! 02! 03! 04
2: The toString() method converts a logical value into a string and returns the result
var a= ["00", "01", "02", "03", "04"] var c = a.toString(); //Convert array to string console.log(c) console.log(typeof c); //Returns a string, indicating that it is a string type //Print results 00,01,02,03,04
The toString() method cannot specify a delimiter, but we can specify a replacement through the replace() method
var a= ["00", "01", "02", "03", "04"] var f = a.toString().replace(/,/gi,'-') console.log(f) //Print result: 00-01-02-03-04
3: toLocaleString()
Converts an array to a locally agreed string
var a= ["00", "01", "02", "03", "04"] var e = a.toLocaleString(); console.log(e) console.log(typeof e); //Print result: 00,01,02,03,04
demo
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script> <title></title> </head> <body> </body> <script type="text/javascript"> var a= ["00", "01", "02", "03", "04"] //1: var b= a.join(',') console.log(b) console.log( typeof b) //2: var c = a.toString(); //Convert array to string console.log(c) console.log(typeof c); //Returns a string, indicating that it is a string type //3: var d = a.join(); //Convert array to string console.log(d) console.log(typeof d); //Returns a string, indicating that it is a string type //4: var e = a.toLocaleString(); //Convert array to string console.log(e) console.log(typeof e); //Returns a string, indicating that it is a string type </script> </html>
2: String to array (2 methods)
String method | explain |
---|---|
split() method | Converts a string to an array |
Extended operator (...) | Extension operators in es6 |
String to array
1: The split () method is used to split a string into an array of strings
It is also used to divide a string into string arrays. What is the difference between split(','), split(), split('')?
split() method | explain |
---|---|
split(',') | |
split() | It can be understood as directly changing into a string, separated by commas by default |
split('') empty string | Each character is divided |
var arr = 'aa,bb,cc,dd' var newStr = arr.split(',') console.log(newStr) // Print result: ["aa", "bb", "cc", "dd"]
var arr = 'aa,bb,cc,dd' var newStr = arr.split() console.log(newStr) // Print result: [aa,bb,cc,dd]
If an empty string ("") is used as a separator, each character in the stringObject will be split
var arr = 'aa,bb,cc,dd' var newStr = arr.split('') console.log(newStr) //Print results: [a "," a "," B "," B "," C "," C "," d "," D]
2: Extension operators in es6
var arr = 'aa,bb,cc,dd' var newStr = [...arr] console.log(newStr) //Print results ["a", "a", "B", "B", "C", "C", "d", "d"]
The above are several methods to convert js array and string ~ ∠ (° ω °)/ ~