String

methods

The query

str.charAt(index)
Copy the code

The first character position is 0 and returns the character at the specified index

str.indexOf("Retrieve string"."Location to begin retrieval")
Copy the code

Returns the position of the given element’s first occurrence in the string, or -1 if none occurs. You can accept a second argument that indicates the start of the search

str.lastIndexOf("Retrieve string"."Location to begin retrieval")
Copy the code

Returns the position of the given element’s last occurrence in the string, or -1 if none occurred

str.search("Retrieve string");
Copy the code

Returns the first location found. The difference is that search can be matched with regular expressions

Returns a Boolean value indicating whether to include startsWith(), endsWith(), and includes()

str.startsWith("foo".1))
Copy the code

Checks for matches that begin at index 0, and the second argument indicates the location where the search begins

str.endsWith("baz")
Copy the code

Check matches starting at index (string.length-substring.length)

str.includes("bar".1))
Copy the code

Check the entire string for the second argument, which indicates where the search began

s.fromCharCode()    // This method takes a series of Unicode code points and returns the corresponding string.
s.charCodeAt(index)    // Returns the Unicode code point (in decimal) for the character at the given position
Copy the code
s.match(regexp)
Copy the code

Used to determine whether the original string matches a substring, returns an array of matched first strings. If no match is found, null is returned.

Splicing/interception

str1.concat(str2,str3)
Copy the code

Concatenate two or more strings

var n = str.replace("Microsoft" ,"Roon")
Copy the code

A substring used to replace a match, usually only the first match (unless a regular expression with the G modifier is used)

var n = str.slice(start,end);
Copy the code

Use to retrieve a substring from the original string and return it without changing the original string. The first argument is the start of the substring, and the second argument is the end of the substring (excluding that position). If the argument is negative, it represents the position of the countdown from the end, that is, the negative value plus the length of the string

var n = str.substr(start,lenght)
Copy the code

Use to retrieve a substring from the original string and return it without changing the original string. The first argument is the start position of the substring, and the second argument is the length of the substring. If the first argument is negative, it represents the character position of the reciprocal calculation. If the second argument is negative, it is automatically converted to 0, so an empty string is returned.

var n = str.substring(start,end)
Copy the code

Use to retrieve a substring from the original string and return it without changing the original string. The first argument represents the start position of the substring, and the second position represents the end position.

var arr = str.split(",")
Copy the code

Returns an array of substrings split according to the given rules. You can also pass in a second argument that determines the number of members to return.

Go to the space

str.trim() // Remove whitespace from both sides of the string
trimeLeft()// The string begins to clean up Spaces
trimRight() // Clean up Spaces at the end.
Copy the code

Fill character

// If the length is smaller than the specified length, the corresponding side is filled with characters until the length condition is met
// The first argument to both methods is length, and the second argument is an optional padding string, which defaults to a space
stringValue.padStart(9.".")//"......foo"
stringValue.padEnd(6)//"foo......"
Copy the code

Case conversion

s.toLowerCase()  // Use to convert a string to lowercase and return a new string without changing the original string.
s.toUpperCase()  // All uppercase
s.localeCompare(s2)  // Compare two strings. It returns an integer that, if less than 0, means that the first string is less than the second string; If it's 0, it means they're equal to each other; If greater than 0, the first string is greater than the second string.
Copy the code