A comprehensive summary of new string manipulation methods in JS+ES6, a total of forty-seven methods

Let me introduce to you how to operate strings. Most of what you know and don’t know is here!

The classification may be a little wrong, please forgive me!

Added

1.concat()

Splicing strings can concatenate two or more strings

 let str = "hello"
    let str1 = "str"
    console.log("hello".concat(str1)) //hello str
    console.log(str.concat(str1)) //hello str

2.padStart(total length, “padded string”)

Returns a new string, which means using the parameter string to complete the original string from the head (left side)

 let str = "hello str"
    // Now the length of our str is 9
    // We limit the total length to 10. We add at the beginning of the string until the total length is 10.
    // If the string is not filled, the string remains unchanged
    console.log(str.padStart(10,"123")) //1hello str

3.padEnd(total length, “padded string”)

Returns a new string, which means using the parameter string to complete the original string from the end (right side)

 let str = "hello str"
    // Now the length of our str is 9
    // We limit the total length to 10. We add at the end of the string until the total length is 10.
    // If the string is not filled, the string remains unchanged
    console.log(str.padEnd(10, "123")) //hello str1

———————————–

Delete

4.slice()

Specified content can be intercepted from the string
The original string will not be affected
The first parameter, the index of the starting position (including the starting position)
The second parameter, the index of the end position (excluding the end position)

 let str = "hello str"
    // One parameter intercepts all subsequent strings
    console.log(str.slice(6)) //str
    console.log(str.slice(0,5)) //hello
    // You can also pass a negative number, which will be calculated later
    console.log(str.slice(0,-1)) //hello st

5.substr()

Intercept the string. The first parameter: the index of the interception starting position. The second parameter: the interception length

 let str = "hello str"
    console.log(str.substr(0,5))//hello
    console.log(str.substr(6,3))//str

6.substring()

String interception substring (starting index number [, ending index number])

 let str = "hello str"
    // When there is only one parameter, all index positions are intercepted, including the index position.
    console.log(str.substring(6)) //"str"
    // When there are two parameters, the starting index position and the ending index position
    //The ending index does not contain the part you want to intercept
    // Remember to wrap the front and not the back
    console.log(str.substring(6,7)) //"s"

7.trim()

Clear spaces at the beginning and end of the string

 let str = " hello str "
    console.log(str.trim()) //"hello str"

8.trimEnd()

The trimEnd() method removes whitespace characters from the end of a string

 let str = "hello str "
    console.log(str.length) //13
    console.log(str.trimEnd().length) // 9
    console.log(str.trimEnd()) // hello str

9.trimRight()

trimRight() method removes whitespace characters from the end of a string

 let str = "hello str "
    console.log(str.length) //13
    console.log(str.trimRight().length) // 9
    console.log(str.trimRight()) // hello str

10.trimLeft()

The trimLeft() method removes whitespace characters from the front of a string

 let str = " hello str"
    console.log(str.length) //13
    console.log(str.trimLeft().length) // 9
    console.log(str.trimLeft()) // hello str

11.trimStart()

The trimStart() method removes whitespace characters from the front of a string

 let str = " hello str"
    console.log(str.length) //13
    console.log(str.trimStart().length) // 9
    console.log(str.trimStart()) // hello str

———————————————-

Change

12.split()

Cut string into array
The cut string can use the array method

 let str = "red,orange"
    console.log(str.split(",")) // ['red', 'orange']
    console.log(str.split("")) //['r', 'e', 'd', ',', 'o', 'r', 'a', 'n', 'g' , 'e']

12.toLocaleLowerCase()

Convert uppercase to lowercase

 let str = "HELLO STR"
    console.log(str.toLocaleLowerCase()) //hello str

13.toLocaleUpperCase()

Convert lowercase to uppercase

 let str = "hello str"
    console.log(str.toLocaleUpperCase()) //HELLO STR

14.toLowerCase()

Convert uppercase to lowercase

 let str = "HELLO STR"
    console.log(str.toLowerCase()) //hello str

16.toUpperCase()

Lowercase to uppercase

 let str = "hello str"
    console.log(str.toUpperCase()) //HELLO STR

17.toString()

There are many uses of toString(). I will introduce the usage related to strings.
The data type can be converted to string type

 let arr = [1]
    let obj = {<!-- -->name:"zs"}
    let num = 1
    console.log(arr.toString()) // 1
    console.log(typeof arr.toString()) // string
    console.log(obj.toString()) // [object Object]
    console.log(typeof obj.toString()) // string
    console.log(num.toString()) // 1
    console.log(typeof obj.toString()) // string

18.replace()

You can replace the specified content in the string with new content
The content of the first parameter to be replaced
New content of the second parameter

 let str = "hello str"
    console.log(str.replace("hello","hh")) //hh str

19.replaceAll()

The replaceAll() method is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression. This function will replace all matching substrings.

 let str = "hello str hello str"
    console.log(str.replaceAll("hello","hh")) //hh str hh str

20.repeat(number)

Returns a new string, which means repeating the string a specified number of times and returning it

 let str = "hello str"
    console.log(str.repeat(2)) //hello strhello str

———————————————-

Check

21.length

Used to get the length of a string

 let str = "hello str"
    console.log(str.length) //9

22.charAt()

str.charAt(index), index is the position index of the string where the character is located, and the index starts from 0
If the position index is exceeded, return ” “

 let str = "hello str"
    console.log(str.charAt(0)) //h
    console.log(str.charAt(9)) //Empty

23.indexOf()

Retrieve whether a string contains specified content
If the content is contained in the string, the index of its first occurrence will be returned
There is a second parameter, which represents the position to start searching for the index
The specified content was not found and returned -1

 let str = "hello hello"
    console.log(str.indexOf("h")) //0
    console.log(str.indexOf("v")) //-1
    //When there are two parameters, the index specifies the starting search position
    console.log(str.indexOf("e",0)) //1
    console.log(str.indexOf("h",1)) //-1

24.lastIndexOf()

Retrieve whether a string contains specified content
Look from back to front
If the content is contained in the string, the index of its last occurrence will be returned
There is a second parameter, which represents the position to start searching for the index
The specified content was not found and returned -1

 let str = "hello hello"
    console.log(str.lastIndexOf("h")) //6
    console.log(str.lastIndexOf("v")) //-1
    // Search from the back to the front starting from the position with index 0
    console.log(str.lastIndexOf("h",0)) //0
    console.log(str.lastIndexOf("h",6)) //6

25.includes()

Used to determine whether a string is contained in another string, returning true or false depending on the situation.

 let str = "hello str"
    console.log(str.includes("hello")) //true
    console.log(str.includes("str")) //true
    console.log(str.includes("helo")) //false
    //When there are two values, the second value is the index. For example, start searching after index 1.
    console.log(str.includes("hello",1)) //false
    console.log(str.includes("ello",1)) //true

26.charCodeAt()

Returns the Unicode value of the character at the specified index position

 let str = "hello str"
    console.log(str.charCodeAt(0)) //104

27.formCharCode()

You can get characters based on character encoding
Need to use String.fromCharCode()

 console.log(String.fromCharCode(20000)) // North

28.search()

You can search whether the string contains specified content
If the specified content is searched, the index of the first occurrence will be returned. If no search is found, -1 will be returned
You can also receive a regular expression as a parameter, and then retrieve the string based on the regular expression

 let str = "hello str"
    console.log(str.search("hello")) //0
    console.log(str.search("str")) //6
    console.log(str.search("v")) //-1
    // Regular expressions can be used
    let str1 = "aac abc acc"
    console.log(str1.search(/a[abc]c/)) //0

29.match()

Can extract qualified content from a string based on regular expressions
By default, only the content requested by the first symbol will be found. Stop retrieval when found
Can set regular expression global matching
Return an array

 let str = "1a2b3c"
    console.log(str.match(/[A-z]/))
    console.log(str.match(/[A-z]/g))

30.at()

Theat() method is used to receive an integer value and return the element corresponding to the index. Positive and negative numbers are allowed. Negative integers count down from the last element in the array.
Matches the element in the array at the given index. If the specified index is not found, undefined is returned.

 let str = "hello str"
    console.log(str.at(9)) // undefined
    console.log(str.at(0)) // h
    console.log(str.at(-1)) // r

31.startsWith()

Returns a Boolean value to determine whether the parameter string is at the head of the original string

 let str = "hello str"
    // h returns true at the head of the string
    console.log(str.startsWith("h"))//true
    // e is not at the head and returns false
    console.log(str.startsWith("e"))//false

32.endsWith()

Returns a Boolean value to determine whether the parameter string is at the end of the original string

 let str = "hello str"
    // r returns true at the end of the string
    console.log(str.endsWith("r")) // true
    // h is not at the end of the string and returns false
    console.log(str.endsWith("h")) // false

33.localeCompare()

Used to compare two strings
Returns 1 if the first string is greater than the second string
Returns 0 if the first string is equal to the second string
If the first string is less than the second string return -1

 console.log("a".localeCompare("b")) // -1
    console.log("a".localeCompare("a")) // 0
    console.log("b".localeCompare("a")) // 1

———————————————-

Tag type method

34.sub()

This method returns a string tagged with sub

 let str = "hello str"
    console.log(str.sub())

35.big()

This method returns a string tagged with big

 let str = "hello str"
    console.log(str.big())

36.bold()

This method returns a string tagged with b

 let str = "hello str"
    console.log(str.big())

37.anchor()

The anchor() method is used to create HTML anchors
This method returns a string tagged with a

 let str = "hello str"
    console.log(str.anchor("url address"))

38.blink()

The blink() method is used to display a blinking string
This method returns a string tagged with blink

 let str = "hello str"
    console.log(str.blink())

39.fixed()

Thefixed() method is used to display a string in typewriter font.
This method returns a string with the tt tag

 let str = "hello str"
    console.log(str.fixed())

40.small()

This method returns a string tagged with small**

 let str = "hello str"
    console.log(str.small())

41.link()

This method returns a string tagged with a

 let str = "hello str"
    console.log(str.link("url address"))

42.strike()

This method returns a string with the strike tag

 let str = "hello str"
    console.log(str.strike())

43.fontcolor()

The fontcolor() method is used to display strings according to the specified color
This method returns a string tagged with font

 let str = "hello str"
    console.log(str.fontcolor("red"))

44.fontsize()

The fontsize() method is used to display a string according to the specified size.
This method returns a string tagged with font

 let str = "hello str"
    console.log(str.fontsize(12))

45.italics()

Theitalics() method is used to display strings in italics.
This method returns an i-tagged string

 let str = "hello str"
    console.log(str.fontsize(12))


———————————————-

46.valueOf()

Return the original value of the String object:

 let str = "hello str"
    console.log(str.valueOf()) //hello str

47. Template string

ES6 adds new template strings, making it easier for us to splice strings
Note: Remember to use backticks as string delimiters to delimit literals
1. Splicing
Before our template string appeared, it was more troublesome for us to splice strings.

 const name = "Jack";
const age = 18;
// Previous concatenated string
console.log("Hello everyone, my name is " + name + ", this year I am " + age + "years old")

Now that we have template strings, splicing them together will be much easier
Template strings use backticks “ as string delimiters to separate literals.

 const name = "Jack";
    const age = 18;
    // Template string, remember to use backticks
    console.log(`Hello everyone, my name is ${<!-- -->name}, and I am ${<!-- -->age} this year`)

2. Line break
In the past, when we used double quotes or single quotes to write strings, if one line did not fit, we needed to use the + sign to splice them.

 const name = "Jack";
    const age = 18;
    // If it doesn’t fit in one line, we need to use + to splice it.
    console.log("Hello everyone, my name is Jack"
     + "I am 18 years old this year")

Now that we have template strings, we can more easily solve the problem of line breaks.

 const name = "Jack";
    const age = 18;
    // If it doesn’t fit in one line, we need to use + to splice it.
    console.log(`Hello everyone, my name is Jack
    I am 18 years old this year`)

Thank you everyone for reading. If there is anything wrong, you can tell me. Thank you everyone!