Manipulation of Strings and Numbers in Javascript

  • 2021-07-13 04:05:47
  • OfStack

1. length Returns the length of a string


 ' abcd'.length; //4 

2. Math.ceil(num) Rounding up, no matter what is after the decimal point, even if. 00001, it will go up by one place.


Math.ceil(25.9); //26 
Math.ceil(25.5); //26 
Math.ceil(25.1); //26 

3. Math.floor(num) Rounding down, no matter what is after the decimal point, even if it is. 99999, it will be subtracted by one place.


Math.floor(25.9); //25 
Math.floor(25.5); //25 
Math.floor(25.1); //25 

4. Math.round(num) 4 houses and 5 entrances.


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) Returns the largest of the specified numbers [until ECMASCript v3, this method has only two parameters].


Math.max(1,32,4,5,6,7,87) //87 

7. Math.min(n,n1,n2) Returns the smallest of the specified numbers [until ECMASCript v3, this method has only two parameters].


Math.max(1,32,4,5,6,7,87) //1 

8. Math.random() Random number, such as returning a random number between specified numbers, the formula is: Math.random()*(y-x) + x


// Return 0-1 The random number between, with decimal by default  
Math.random(); 
// Only return below 0 And 1 Random numbers, remember yes and no 0 Is 1 
Math.round( Math.random() ) 
//5-10 Random number of  
Math.round( Math.random()*5 + 5 ) 
//10-20 Random number of  
Math.round( Math.random()*10 + 10 ) 
//0-100 Random number of  
Math.round( Math.random()*100 ) 

Math there are many methods, other not commonly used, here will not be introduced, the specific can see this article.

9. charAt() Returns the character at the specified position.


 ' abcd'.charAt(0); //a 
 ' abcd'.charAt(1); //b 
 ' abcd'.charAt(2); //c 

10, Math.ceil(num) 0 Gets the encoding of the character at the specified position ( unicodo Coding).


 ' abcd'.charCodeAt(1) //98 
 ' abcd'.charCodeAt(2) //99 

11, fromCharCode() One or more of the specified Unicode Value, and then returns a string.


Math.ceil(25.9); //26 
Math.ceil(25.5); //26 
Math.ceil(25.1); //26 
0

12, indexOf() Returns the index that occurs at the first place of a substring in a string. Returns-1 if there is no match.


Math.ceil(25.9); //26 
Math.ceil(25.5); //26 
Math.ceil(25.1); //26 
1

13. substring() If it is 1 parameter, the subscript is returned from the start Characters from beginning to end at; If it is two parameters, the extract string is derived from the start To end-1 The character between.


Math.ceil(25.9); //26 
Math.ceil(25.5); //26 
Math.ceil(25.1); //26 
2

14, substr() If it is 1 parameter, the subscript is returned from the start Characters from beginning to end at; If it is two parameters, the extract string is derived from the start Position start interception end Bit.


 ' abcd'.substr(3) //d 
 ' abcd'.substr(0,2) //ab 
 ' abcd'.substr(1,2) //bc 

15, slice() If it is 1 parameter, the subscript is returned from the start The characters from the beginning to the end, if it is a negative number, take several characters from the back; If it is two parameters, the extract string is derived from the start To end-1 If the first one is negative, then the second one cannot be less than the position of the first character, and this comparison wraps around.

See the following code for details.


Math.ceil(25.9); //26 
Math.ceil(25.5); //26 
Math.ceil(25.1); //26 
4

16, replace() String replacement.


Math.ceil(25.9); //26 
Math.ceil(25.5); //26 
Math.ceil(25.1); //26 
5

17, split() Strings are divided into arrays. If there is one parameter, it is divided according to the parameter, and if there are two, it is divided according to the second parameter


Math.ceil(25.9); //26 
Math.ceil(25.5); //26 
Math.ceil(25.1); //26 
6

18, toUpperCase() Convert lowercase strings to uppercase


Math.ceil(25.9); //26 
Math.ceil(25.5); //26 
Math.ceil(25.1); //26 
7

19, toUpperCase() Convert uppercase strings to lowercase


 ' ABCD'.toUpperCase( " ); //abcd 

20, Math.round(num)1 The string is converted to a number, and stops when it encounters a non-number. The first character cannot be converted to a number, and returns Math.round(num)2


Math.ceil(25.9); //26 
Math.ceil(25.5); //26 
Math.ceil(25.1); //26 
9

21, parseFloat() The string is converted to a decimal, and stops when it encounters a non-number. The first character cannot be converted to a number, and returns Math.round(num)2


parseInt('10') //10 
parseInt('10abc') //10 
parseInt( ' 10.256 ' ) //10.256 
parseInt( ' 10.25W6 ' ) //10.25 
parseInt( ' W60 ' ) //NaN 

Summarize

The above is about js string and the number of the operation method of the whole content, I hope that the content of this article to everyone's study or work can bring 1 set of help, if you have any questions, you can leave a message to communicate.


Related articles: