⭐
A:
Operation method
Can add, delete, change, check the dimension to sum up.
Once a string is created, it cannot be changed. All of the following methods do not change the original string, but return a new string.
increase
Generally, string concatenation is used
// String concatenation
const str1 = 'lin'
const str2 = 'handsome'
str1 + ' is ' + str2 // 'lin is handsome'
`${str1} is ${str2}` // 'lin is handsome'
Copy the code
Of course, the + or ‘ ‘method is not a string method. If you want to use a method, you can use concat. This method is not commonly used, but I’ll show you.
concat
The concat() method merges one or more strings with the original string concatenation to form a new string and returns it.
/ / grammar
str.concat(str2, [, ...strN])
Copy the code
const str1 = 'lin'
const str2 = 'handsome'
str1.concat(' is ').concat(str2) // 'lin is handsome'
// String concatenation is more convenient than string concatenation.
Copy the code
const greetList = ['Hello'.' '.'Venkat'.'! ']
' '.concat(... greetList)// 'Hello Venkat! '
greetList.join(' ') // 'Hello Venkat! '
// In this case it is better to use the array join method.
Copy the code
delete
- slice
- substring
slice
The slice() method extracts a portion of a string and returns a new string, taking one or two arguments.
The new string returned is determined by the indexStart and indexEnd arguments (including indexStart and indexEnd).
Only one argument is passed, and indexEnd defaults to string length.
If the argument is negative, it is equivalent to index + strLength
const str = 'hello world! '
str.slice(0.4) // 'hell'
str.slice(2) // 'llo world'
str.slice(-5, -1) // 'worl' equals str.slice(6, 10)
str.slice(6.10) // 'worl'
Copy the code
substring
The substring() method works almost the same as the slice method, with a few minor differences in the rules for intercepting strings,
- If indexStart equals indexEnd, subString returns an empty string.
- If indexEnd is omitted, subString extracts characters all the way to the end of the string.
- If any parameter is greater than strLength, it is treated as strLength.
- If indexStart is greater than indexEnd, subString is executed as if the two arguments were swapped.
(Different from Slice)
- If either parameter is less than 0 or NaN, it is treated as 0.
(Different from Slice)
const str = 'hello world'
str.slice(4.4) / /"
str.substring(4.4) / /"
str.slice(2) // 'llo world'
str.substring(2) // 'llo world'
str.slice(4.100) // 'o world'
str.substring(4.100) // 'o world'
str.slice(4.2) / /"
str.substring(4.2) // 'll'
str.slice(4, -2) // 'o wor'
str.substring(4, -2) // 'hell'
Copy the code
There is also a substr method, which is almost obsolete. Do not use it, see MDN.
change
- trim
- repeat
- padEnd
- ToLowerCase, toUpperCase
trim
The trim() method removes whitespace characters from both ends of a string.
The trimStart() method removes whitespace from the beginning of the string. TrimLeft () is an alias for this method.
The trimEnd() method removes whitespace characters from the end of a string. TrimRight () is an alias for this method.
const str = ' hello world! '
str.trim() // 'hello world! '
str.trimLeft() // 'hello world! '
str.trimRight() // ' hello world! '
Copy the code
repeat
It takes an argument n to copy the string n times, and returns the string after concatenating all copies. N is automatically converted to an integer.
const str = 'abc'
str.repeat(-1) // RangeError: repeat count must be positive and less than inifinity
str.repeat(0) / /"
str.repeat(1) // 'abc'
str.repeat(2) // 'abcabc'
str.repeat(3.5) // the 'abcabcabc' argument count will be automatically converted to an integer.
str.repeat(1/0) // RangeError: repeat count must be positive and less than inifinity
Copy the code
PadEnd, padStart
The padEnd() method populates the current string with a string (or repeats if necessary) and returns the string that has been filled to the specified length. Populate from the end of the current string (right).
The padStart() method fills in from the left
const str = 'hello'
str.padEnd(10.'. ') // 'hello..... '
str.padStart(10.'. ') // '.....hello'
Copy the code
ToLowerCase, toUpperCase
Case conversion
const str = 'Chinese simplified useful - CN | | useful - Hans'
str.toLowerCase() Chinese simplified useful - cn / / '| | useful - Hans'
Copy the code
const str = 'hello'
str.toUpperCase() // 'HELLO'
Copy the code
check
- chatAt()
- IndexOf, lastIndexOf
- Includes, startsWith, endsWith
charAt
The charAt() method returns the specified subscript character from a string.
const str = 'hello'
str.charAt(1) // 'e'
Copy the code
IndexOf, lastIndexOf
The indexOf() method searches for the incoming string from the beginning of the string and returns the position. The lastIndexOf() method looks from the back to the front.
const str = 'hello'
str.indexOf('e') / / 1
str.indexOf('l') // 2 if there are duplicate elements, return the first one
str.indexOf('xx') // -1 If not found, -1 is returned
str.lastIndexOf('l') / / 3
Copy the code
Includes, startsWith, endsWith
The includes() method is used to determine whether one string is included in another string, returning true or false depending on the case.
The startsWith() method is used to determine whether the current string startsWith another given substring.
The endWith() method determines whether the current string ends with another given substring.
const str = 'hello, nice to meet you'
str.includes('nice') // true
str.startsWith('hello') // true
str.endsWith('you') // true
Copy the code
Conversion method
split
The split() method splits the string into each item in the array according to the specified separator.
const str = 'Paul,Booker,Ayton,Bridges,Johnson'
str.split(', ') // ['Paul', 'Booker', 'Ayton', 'Bridges', 'Johnson']
Copy the code
Regular expression method
Several methods have been designed for regular expressions on strings
- match()
- search()
- replace()
match
The match() method retrieves the result of a string matching a regular expression.
The match() method takes an argument, either a regular expression string or a RegExp object, and returns an array.
const str = 'find, mind, wind'
const pattern = /.ind/g
const matches = str.match(pattern)
console.log(matches) // ['find', 'mind', 'wind']
Copy the code
search
The search() method performs a search match between a regular expression and a string. Returns the matching index if found, -1 otherwise.
const str = 'find, mind, wind'
str.search(/ind/) / / 1
str.search(/xx/) // -1
Copy the code
replace
Takes two arguments, the first to match the content and the second to replace the element (available function)
const str = 'find, mind, wind'
str.replace('ind'.'abc') // 'fabc, mind, wind' will only replace the first one found
str.replace(/ind/g.'abc') // 'fabc, mabc, wabc' replaces all
Copy the code
At the end
If my article is helpful to you, your 👍 is my biggest support ^_^
I’m Allyn, export insight technology, goodbye!
The last:
How to break the forEach loop in JavaScript?
Next up:
“Front End daily Question (16)” The difference between deep copy and shallow copy?