String: a global object is a constructor for a string or a sequence of characters.
The following lists several properties and methods commonly used by String objects.
Properties:
- The length property indicates the length of a string.
let string="hello" document.write(string.length)//Return to 5
method:
- The charAt() method returns the specified character from a string. Index of characters in parentheses
let string="hello" document.write(string.charAt(2))//Return l
- The concat() method combines one or more strings with the original string to form a new string and return it.
let string="hello" console.log(string.concat(",girl"))//Return to hello,girl
- The endsWith() method is used to judge whether the current string ends with another given substring, and returns true or false according to the judgment result. Fill in the substring in parentheses
let string="hello" console.log(string.endsWith("o"))//true
- The startsWith() method is used to judge whether the current string starts with another given substring, and returns true or false according to the judgment result.
let string="boy girl good right" console.log(string.startsWith('boy')) //true
- The includes() method is used to determine whether a string is contained in another string, and returns true or false according to the situation. The includes() method is case sensitive.
Syntax: str.includes(searchString[, position])
searchString: the string to search in this string.
Position: optional, which index position of the current string to start searching for substrings. The default value is 0.
let string="hello,world" console.log(string.includes("world"))//true
- The indexOf() method returns the index of the specified value that appears for the first time in the String object calling it, and searches from fromIndex (a number indicating the location where to start searching). If the value is not found, - 1 is returned.
let string="hello" console.log(string.indexOf("l"))//Returns the index 2 of the location where l first appeared
- The lastIndexOf() method returns the index of the last occurrence of the specified value of the calling String object, and searches from back to front at the specified position fromIndex (a number indicating the starting position) in a String. If this specific value is not found, - 1 is returned.
let string="hello" console.log(string.lastIndexOf("l"))//Returns l the index 3 of the last occurrence
- The match() method retrieves and returns the result of a string matching regular expression (RegExp object is used to match text with a pattern).
let string="Hello123456" let reg=/[a-zA-Z]/g //The regular expression matches the upper and lower case letters. g indicates full-text search. If g is not added, the search will end when the first result matching the regular expression is found console.log(string.match(reg))// Returns an array ['h ',' e ',' l ',' l ',' o ']
- The padEnd() method will fill the current string with a string (repeat the filling if necessary) and return the filled string of the specified length. Fills from the end (right) of the current string.
let string="hello" console.log(string.padEnd(10,'1'))//Indicates that the length of the string is filled to 10 and filled with string '1' //Return result: "hello11111"
- The padStart() method fills the current string with another string (repeated multiple times if necessary) so that the resulting string reaches the given length. Fills from the left side of the current string.
let string="hello" console.log(string.padStart(10,'1'))//Indicates that the length of the string is filled to 10 and filled with string '1' //The result is returned as: "11111 hello"
- repeat() repeats the current string several times.
let string="hello" console.log(string.repeat(3))//hellohellohello
- replace(), two parameters. Replace only one before replacing
let string="hello hello world" console.log(string.replace('hello','world'))//world hello world
- replaceAll() replace, replace all
let string="hello hello world" console.log(string.replaceAll('hello','world'))//world world world
- search() finds the specified character in the string and returns the index of the first match in the string; Otherwise, - 1 is returned.
let string="hello world" console.log(string.search('hello'))//0 console.log(string.search('boy'))//-1
- slice() method extracts a part of a string and returns a new string without changing the original string.
Syntax: str.slice(beginIndex[, endIndex])
let string="helloworld" console.log(string.slice(3,8))//lowor
- The split() method uses the specified separator String to split a String object = = into a substring array, and = = determines the position of each split with a specified split String.
let string="boy girl good right" console.log(string.split(' ')) //Split strings with spaces //Return ['boy', 'girl', 'good', 'right']
- The substring() method returns a subset of a string from the start index to the end index, or from the start index to the end of the string. intercept
Syntax: str.substring(indexStart[, indexEnd])
indexStart: the index of the first character to be intercepted. The character at the index position is used as the first letter of the returned string.
indexEnd: optional. An integer between 0 and the length of the string. The characters indexed by this number are not included in the intercepted string.
let string="boygirlgoodright" console.log(string.substring(8,14)) //oodrig
- toLowerCase() converts the string value that calls the method to lowercase and returns it.
let string="HHH" console.log(string.toLowerCase())//hhh
- The toUpperCase() method converts the string calling the method to uppercase and returns
let string="hhh" console.log(string.toUpperCase())//HHH
- The trim() method removes white space characters from both ends of a string.
let string=" boy " console.log(string.trim()) //"boy" ` insert code slice here`
- The trimEnd() method removes white space characters from the end of a string. Trimlight () is an alias for this method.
let string=" boy " console.log(string.trimEnd()) //" boy"
- The trimStart() method removes spaces from the beginning of a string. trimLeft() is an alias for this method.
let string=" boy " console.log(string.trimStart()) //"boy "
- The valueOf() method returns the original value of the String object
let string="boy " console.log(string.valueOf()) //Returns the string value boy