1.indexOf() method ----This method is more commonly used
Returns the location where a specified string value first appears in the string
Use format: stringObject.indexOf(substring, startpos)
For example: Find the second o location
var mystr="Hello World!"
document.write(mystr.indexOf("o",mystr.indexOf("o")+1));------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
mystr.indexOf("o") -----The result is 4, start looking for it from the following table of the string.
document.write(mystr.indexOf("o",5)); start looking at the fifth position.
The result is: 7
Note: The .indexOf() method is case sensitive.
.If the string value to be retrieved does not appear, the method returns -1.・
2. String split()
The split() method splits the string into a string array and returns this array.
grammar:
stringObject.split(separator,limit)
Note: If an empty string ("") is used as a separator, each character in the stringObject will be split between.
For example:
var mystr="86-010-85468578";
Separate string objects by character "-" mystr
document.write( mystr.split("-") + "<br />");---Result 86,010,85468578
Split each character of the string object mystr
document.write( mystr.split("") + "<br />");---Result: 8,6,-,0,1,0,-,8,5,4,6,8,5,7,8
Split the string object mystr into characters, split 3 times
mystr.split("",3) -------------Results 8,6,-
3. Extract the string substring()
The substring() method is used to extract characters in a string between two specified subscripts.
grammar:
stringObject.substring(starPos,stopPos)
Notice:
1. The returned content is all characters from start (including characters at the start position) to stop-1, and its length is stop minus start.
2. If the parameter start is equal to stop, then the method returns an empty string (that is, a string with length 0).
3. If start is larger than stop, the method will swap these two parameters before extracting the substring.
For example:
var mystr="Hello World!"
document.write( mystr.substring(6) + "<br />");----Seal all characters starting from 6 in the subscript---The result is World!
document.write( mystr.substring(5,4) );
document.write( mystr.substring(4,5) );------The result is all o The number of digits taken is 5-4
4. Extract the specified number of characters substr()
The substr() method extracts the specified number of strings starting from the startPos position from the string.
grammar:
stringObject.substr(startPos,length)
Note: If the parameter startPos is a negative number, the position from the end of the string is calculated. That is, -1 refers to the last character in the string, -2 refers to the second to last character, and so on.
If startPos is negative and the absolute value is greater than the string length, startPos is 0
Example: var mystr="Hello World!";
document.write( mystr.substr(6) + "<br />");-----------------Intercept World
document.write( mystr.substr(0,5) );---Intercept----Hello
5.Math
Round ()
Math.round(x)
The random() method can return a random number between 0 and 1 (greater than or equal to 0 but less than 1).
Math.random();
6.Array array object method
The concat() method is used to concatenate two or more arrays. This method returns a new array without changing the original array.
grammar
arrayObject.concat(array1,array2,...,arrayN)
Note: This method does not change the existing array, but only returns a copy of the connected array.
var myarr1= new Array("010")
var myarr2= new Array("-","84697581");
var mycon=myarr1.concat(myarr2);
document.write(mycon) ---Output 010,-,84697581
The description is just a copy.
The join() method is used to put all elements in the array into a string. Elements are separated by specified delimiters.
var myarr1= new Array("86","010")
var myarr2= new Array("84697581");
var myarr3= myarr1.concat(myarr2);
document.write(myarr3.join("")+"<br/>");------ The result is 8601084697581
document.write(myarr3.join("_")+"<br/>"); -----The result is: 86_010_84697581
document.write(myarr3.join()+"<br/>");-----The result is 86,010,84697581 Default is, number separated
7. Reverse()
The reverse() method is used to reverse the order of elements in an array.
var myarr1= ["I","Love","You"];
document.write(myarr1.reverse());----The result is you, love, I'm careful not to reserve. Don't write it wrong
8. Selected element slice()
The slice() method returns the selected element from an existing array.
arrayObject.slice(start,end)
1. Returns a new array containing elements in an arrayObject from start to end (excluding this element).
2. This method does not modify the array, but returns a subarray
var myarr1= ["I","Love","You"];
document.write(myarr1.slice(1,3)) ---The result is-Love, you are the same as substring, but this is for the array, the number of fetched is 3-1=2
9. Array sort()
The sort() method makes the elements in the array sort in a certain order.
grammar:
arrayObject.sort(method function)
1. If <Method Function> is not specified, arrange it in unicode code order.
2. If <Method Function> is specified, sort by the sort method specified in <Method Function>.
function sortNum(a,b) {
return ab;
}
var myarr = new Array("80","16","50","6","100","1");
document.write(myarr.sort(sortNum)); --- jThe result is: 1,6,16,50,80,100
If it is ba -- the result is 100,80,50,16,6,1
The above are some commonly used JavaScript built-in objects that I have summarized. I hope everyone likes it.