Writing in the front

JS string and number methods are very important whether in an interview, or in a job, or in an algorithm, maybe an API will allow you to write a lot less logic. This article summarizes the string and number methods in detail, so that you can know their usage in one place.

To eat: Primary front-end taste index: 😋😋😋😋😋

String methods

1. Create a string

First, there are two ways to create strings. Note that the constructor (such as new String(” String “)) creates a wrapper object that encapsulates a primitive type value (such as “String “). The String method does not change the value of the original String, but returns a new String

let stringA = new String('string');
let stringB = 'string';
console.log(typeof stringA);  //object
console.log(typeof stringB);  //string
Copy the code

2. charAt

CharAt: Returns a character at a specified position in a string

let str = 'jueJin';
console.log(str.charAt(2))  //e
Copy the code

3.concat

Concat: Concates two strings and returns a new string

let str = 'zhangsan';
let str2 = str.concat(' juejin');
console.log(str2);  //zhangsan juejin
Copy the code

4. indexOf

IndexOf: Returns the position of the first occurrence of a specified character in a string

let str = 'juejin';
let n = str.indexOf('i');
console.log(n);   / / 4
Copy the code

5. lastIndexOf

LastIndexOf: Searches backwards, otherwise the same as indexOf

let str = 'juejin';
let n = str.lastIndexOf('j');
console.log(n);   / / 3
Copy the code

6. includes

Includes: Queries whether a string includes the specified character substring

let str = 'zhangsan';
console.log(str.includes('san')) //true
Copy the code

7. match

Match: Generally used to find the match of one or more regular expressions. The return value is an array containing all matched values

let str = 'zhangsan123 juejin456'
console.log(str.match(/\d/g));  //['1', '2', '3', '4', '5', '6']
Copy the code

8. repeat

Repeat: Copies the string a specified number of times

let str = 'juejin';
console.log(str.repeat(3));  //juejinjuejinjuejin
Copy the code

9. replace

Replace: Finds a match and replaces it, usually with a regular expression collection.

let str = 'juejin';
let str2 = str.replace(/jue/.'zhangsan ');
console.log(str2);  //zhangsan jin
Copy the code

10. search

Search: searches for a value together with a regular expression. If the value is found, the index value is returned. If the value is not found, -1 is returned

let str = 'juejin123';
let n = str.search(/\d/);
console.log(n)  / / 6
Copy the code

11. slice, substring

Slice, substring: Used to slice a portion of a string, taking the starting index and the ending index +1, and returning a new one.

let str = 'juejin';
let str2 = str.slice(3.6);
let str3 = str.substring(3.6);
console.log(str2, str3);   //jin jin
Copy the code

The difference between Slice and Substring: When receiving a negative argument, Slice adds the length of its string to the corresponding negative number, while Substring simply converts the negative arguments to 0

let str = 'juejin';
console.log(str.slice(-2));  //in
console.log(str.substring(-2));  //juejin
Copy the code

12. substr

Substr: Intercepts a portion of a string, taking the starting index and the number, and returns a new one.

let str = 'juejin';
let str2 = str.substr(3.3)
console.log(str2);  //jin
Copy the code

13. split

Split: To split a string to specify an array of strings

let str = 'zhangsan juejin';
console.log(str.split(' '));  //[ 'zhangsan', 'juejin' ]
Copy the code

14. startsWith

StartsWith: Checks whether the string begins with a specified string

let str = 'kerwin love';
console.log(str.startsWith('kerwin')); //true
Copy the code

15. toLowerCase,toUpperCase

ToLowerCase,toUpperCase: Converts the string to case

let str = 'jueJin';
console.log(str.toLowerCase());  //juejin
console.log(str.toUpperCase());  //JUEJIN
Copy the code

16. trim

Trim: Removes Spaces at both ends of the string

let str = ' jue jin ';
console.log(str.trim());  //jue jin
Copy the code

17. parseInt

ParseInt: Parses a string, returns an integer, and the second argument is the cardinality of the parsed number.

let num = '101';
console.log(parseInt(num, 2));  / / 5
Copy the code

Number method

18. isFinite

IsFinite: Checks whether the parameter passed in is a finite number

let num = 123
console.log(Number.isFinite(num));  //true
Copy the code

19. toExponential

ToExponential: conversion toExponential counting with decimal values

let num = 5.21;
console.log(num.toExponential(1));  / / 5.2 e+0
Copy the code

20. toFixed

ToFixed: Keep x decimal places and convert to a string

let nums = 5.213;
let res = nums.toFixed(1)
console.log(res);  / / 5.2
console.log(typeof res);  //string
Copy the code

21. toPrecision

ToPrecision: Change to the specified length and convert to a string

let num = 133.520;
console.log(num.toPrecision(3))  / / 134
Copy the code

22. toString

ToString: Converts a number to a string. The argument is converted to base

let num = 101;
console.log(num.toString(8));  / / 101
Copy the code

Thank you for reading

Thank you very much for reading to the end, if there is any mistake, I hope you can point out, so as not to mislead others. If you think it is helpful to you, I hope you can click a like, add a concern, any question can contact me, I hope we can make progress together. Finally, I wish you a bright future, we climb each other, high meet 🌈!