js cut

 
split()

split(serparator,howmany)
Cut where specified by the serparator parameter
howmany After split() can have or not refer to the length of the returned array, it will not return the length specified by the parameter

split() splits a string into an array of strings
1. Inside the parentheses is the cut where the parameters are specified
E.g
var str="2:3:4:5"

console.log(str.split(":") ) //Split by colon
//2,3,5,5
2. Empty string (""), then each character of the string will be cut
E.g
var str="My name is wangyuying"

console.log(str.split("") ) //(Double quotes without spaces in parentheses will cut each character of the string)
//M,y, ,n,a,m,e, ,i,s, ,w,a,n,g,y,u,y,i,n,g


var str="My name is wangyuying"

console.log(str.split(" ") ) //The double-quote space in the brackets specifies the space specified by the parameter to be cut
//My,name,is,wangyuying
3. howmany function
var str="wangyuying"


console.log(str.split("", 4))//Cut each string to return each character of the array length, not return the characters after the specified array length
//w,a,n,g

slice()
Returns the selected element from an existing array
Object.slice(start,end)//start starts from where to select where end ends but does not include the element end
E.g
var arr = [1,2,3,4,5]
console.log(arr.slice(0,3)) //All array elements starting from the position specified by 0 to the specified position 3 but not including the specified position 3
//s position
//1,2,3

var arr = [1,2,3,4,5]

console.log(arr.slice(3))//Specify a parameter, then the returned array will contain all array elements from the beginning to the end
//4,5
var arr = [1,2,3,4,5]

console.log(arr.slice(1,-1)) //-1 specifies the last element so start from the position specified by 1 to the last element,
// but not including the last element
//2,3,4

var arr = [1,2,3,4,5]

console.log(arr.slice(-3,-2)) //-3 refers to the third-to-last element -2 refers to the second-to-last element
//3 //So the returned array contains the third-to-last element and does not contain the difference between the second-to-last element
//element
substring()
Extracts the characters in a string between two specified subscripts.
object.substring(start,stop)//start, stop are non-negative integers
E.g:
var str="Hello world!"
console.log(str.substring(3)) //returns subscript 3 and the characters after subscript 3

var str="My name is wyy!"
console.log(str.substring(1,5))//returns the characters from subscript 1 to subscript 5, including subscript 1 character but not including 5 characters
//and na




 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326969774&siteId=291194637
cut