preface
String manipulation: array manipulation: array manipulation: array manipulation: array manipulation: array manipulation: array manipulation: array manipulation: array manipulation: array manipulation: array manipulation
String definition
Strings are wrapped in single or double quotation marks in js
const str = 'hello'
const username = "Leo"
Copy the code
Two, common methods
1. str.length
You can get the following code for the length of the string (example) :
let username = 'leo'
console.log(username.length) / / 3
Copy the code
2. charAt()
If you pass in an index value, you get the following character code for the index value (example) :
let str = 'Hi~ Leo'
console.log(str.charAt(4)) // L
Copy the code
Note: space also counts as a character
3. concat()
The code for concatenating two strings is as follows (example) :
let str = 'Hi~'
let username = 'Leo'
console.log(str.concat(username)) // Hi~Leo
Copy the code
4. includes()
Used to check if a string contains a string, returns a Boolean as shown in the following example:
const str = 'Hello Leo'
console.log(str.includes('Leo')) // true
Copy the code
5. match()
This method returns an array after passing the regular check. The code that fails to return null looks like this (example) :
let username = 'Leo'
let userName = 'leo1'
// Matches a re that begins with a lowercase letter
const reg = /^[a-zA-Z]+$/
console.log(username.match(reg)) // ["Leo", index: 0, input: "Leo", groups: undefined]
console.log(userName.match(reg)) // null
Copy the code
6. replace()
You can change the first qualified character in the string to the specified character code as follows (example) :
let tel = '134, 1234, 1234'
console.log(tel.replace(' '.The '-')) / / 134-1234, 1234
Copy the code
7. replaceAll()
You can replace all qualified characters in a string with the following code:
let tel = '134, 1234, 1234'
console.log(tel.replaceAll(' '.The '-')) / / 134-1234-1234
Copy the code
8. split()
The parentheses pass in what character to split, and finally return an array, which can be retrieved by subscripting the corresponding string code as follows (example) :
const hello = "Hello World";
const helloWorldSplit = hello.split(' ');
console.log(helloWorldSplit); // ["Hello", "World"];
console.log(helloWorldSplit[0]); // "Hello"
Copy the code
9. substring()
Pass in two arguments, the truncated actual index and the truncated termination index code as follows (example) :
const hello = 'Hello World';
console.log(hello.substring(1.4)); // ell
Copy the code
Note: the end index character is not truncated, the second parameter is not passed, the default truncation to the last character
10. toLowerCase()/toUppercase()
The code for cashing a string is as follows (example) :
const username = 'Leo'
console.log(username.toLowerCase()) // leo
console.log(username.toUpperCase()) // LEO
Copy the code
11. trim()
To remove whitespace from the left and right sides of the string, invalidates Spaces in the middle of the string (example) :
const username = ' le o '
console.log(username.trim()) // le o
Copy the code
12. indexOf()
The code for finding the starting position of the index value for the first occurrence of the specified string in a string is as follows (example) :
const str = 'Hi~ My name is Leo,nice to meet you~'
console.log(str.indexOf('Leo')) / / 15
Copy the code
conclusion
Today concludes here ~ breaks