Operations on strings and numbers in Javascript, and operations on javascript
1,length -Returns the length of the string.
‘abcd'.length; //4
2,Math.ceil(num) -Rounded up, regardless of the number after the decimal point, even if it is. 00001, It is rounded up to one place.
Math.ceil(25.9); //26 Math.ceil(25.5); //26 Math.ceil(25.1); //26
3,Math.floor(num)-Rounded down, regardless of the number after the decimal point, even if it is. 99999, it will be dropped by one.
Math.floor(25.9); //25 Math.floor(25.5); //25 Math.floor(25.1); //25
4,Math.round(num)-Rounding.
Math.round(25.9); //26 Math.round(25.5); //26 Math.round(25.1); //25
5,Math.abs(num)-Returns the absolute value of the number.
Math.abs(-10); // 10 Math.abs(10); // 10 Math.abs(-10.5); // 10.5
6,Math.max(n,n1,n2)-Return the maximum value of a specified number [this method has only two parameters before ECMASCript v3].
Math.max(1,32,4,5,6,7,87) //87
7,Math.min(n,n1,n2) -Return the minimum value of the specified number [this method has only two parameters before ECMASCript v3].
Math.max(1,32,4,5,6,7,87) //1
8,Math.random() -Random number. If a random number is returned, the formula is:Math.random()*(y-x) + x
// Returns a random number between 0 and 1. The default value is Math with decimal places. random (); // only the random numbers of 0 and 1 are returned. Remember to be a sum, either 0 or 1 Math. round (Math. random () // random number Math of 5-10. round (Math. random () * 5 + 5) // random number Math of 10-20. round (Math. random () * 10 + 10) // random number Math from 0-100. round (Math. random () * 100)
Math has many other methods that are not commonly used. We will not introduce them here. For details, refer to this article.
9,charAt() -Return the characters at the specified position.
‘abcd'.charAt(0); //a ‘abcd'.charAt(1); //b ‘abcd'.charAt(2); //c
10,charCodeAt() -Get the encoding of characters at the specified position (unicodoEncoding ).
‘abcd'.charCodeAt(1) //98 ‘abcd'.charCodeAt(2) //99
11,fromCharCode()-One or more specifiedUnicode And returns a string.
String.fromCharCode(97) //a String.fromCharCode(97,98,99,100,101,102) //abcdef
12,indexOf() -Returns the index at the first part of a substring in the string. If no match exists,-1 is returned.
‘abcd'.indexOf(‘b') //1 ‘abcd'.indexOf(‘g') //-1
13,substring() -If it is a parameter, the subscript is returned fromstartThe start and end characters. If there are two parameters, extract the string fromstartToend-1.
‘abcd'.substring(3) //d ‘abcd'.substring(0,3) //abc ‘abcd'.substring(1,2) //b
14,substr() -If it is a parameter, the subscript is returned fromstartThe start and end characters. If there are two parameters, extract the string fromstartStart to interceptendBit.
‘abcd'.substr(3) //d ‘abcd'.substr(0,2) //ab ‘abcd'.substr(1,2) //bc
15,slice() -If it is a parameter, the subscript is returned fromstartStart to end. If it is a negative number, the number of characters starting from the end. If it is two parameters, the string is extracted fromstartToend-1If the first character is negative, the second character cannot be smaller than the first character.
For more information, see the following code.
'Abcd '. slice (-1) // d 'abc '. slice (-2) // cd 'abcd '. slice (3) // d 'abc '. slice () // AB 'abcd '. slice () // B // at this time-2, select the location to c, followed by 2, according to the rule, the location should be 2-1, that is, the location of B, therefore, the returned result is 'abc '. slice (-) /// at this time-2, select the location to c, followed by 3, according to the rule, the location should be 3-1, that is, the location of c, therefore, c 'abcd' is returned '. slice (-2, 3) // c
16,replace() -String replacement.
// Replace a with x 'abcd '. replace ('A', 'x') // xbcd // only the content that is matched for the first time can be replaced '. replace ('A', 'x') // xbacad // to replace all matches, use the regular 'abacad '. replace (/a/gi, 'x') // xbxcxd
17,split() -The string is split into arrays. If a parameter is specified, it is divided by parameters. If two parameters are specified, the following parameters are used:
// If it is a null character, each character in the string is split into 'abcd '. split (""); // "a", "B", "c", "d" // if it is a space, it is divided by space. If there is no space, directly output the original string 'AB Cd '. split (""); // "AB", "cd" // if it is a space, it is separated by space. If there is no space, the original string 'abacad' is output directly '. split ("a"); // "", "B", "c", "d" 'abacad '. split ("a", 2); // "," B"
18,toUpperCase() -Converts lowercase strings to uppercase letters.
‘abcd'.toUpperCase(“”); //ABCD
19,toUpperCase() -Converts an uppercase string to a lowercase string.
‘ABCD'.toUpperCase(“”); //abcd
20,parseInt() -When a string is converted to a number and a non-number is stopped, the first character cannot be converted to a number.NaN
parseInt(‘123abc') //123 parseInt(‘abc123′) //NaN
21,parseFloat()-If the string is converted to a decimal number and a non-number is stopped, the first character cannot be converted to a number.NaN
parseInt('10') //10 parseInt('10abc') //10 parseInt(‘10.256′) //10.256 parseInt(‘10.25W6′) //10.25 parseInt(‘W60′) //NaN
Summary
The above is all about the operations on strings and numbers in js. I hope the content in this article will help you in your study or work. If you have any questions, please leave a message.