Home >Web Front-end >JS Tutorial >A Deep Dive into JavaScript String Objects_The Basics
String string object
1. Introduction
String object, perform operations on strings, such as intercepting a substring, searching for strings/characters, converting case, etc.
2. Definition
2.1 new String(Value) constructor: returns a String object whose content is Value
Parameters:
①value {String}: String
Return value:
{String object} returns a String object whose content is Value
Example:
2.2 Direct assignment (recommended)
Example:
3. Instance attributes
3.1 length: Returns the number of characters in the string
4. Instance methods
Note: The instance method of string does not change the string itself, but only returns the result of the operation.
4.1 charAt(index): Returns the character at the specified position in a string. The number starts from 0. If a non-existent value is passed in, an empty string is returned
Parameters:
①index {int}: Position index, calculated from 0
Return value:
{string} returns the character at the specified position in a string; if a non-existent position value is passed in, an empty string is returned
Example:
4.2 charCodeAt(index): Returns the Unicode encoding of the character at the specified position in a string
Parameters:
①index {int}: Position index, calculated from 0
Return value:
{number} Returns the Unicode encoding of the character at the specified position in a string; if a non-existent position value is passed in, NaN
is returnedExample:
4.3 concat(value1, value2 ... valueN): Concatenate one or more strings and return the concatenated string
Parameters:
①value1,value2 ... valueN {string}: one or more strings
Return value:
{string} Returns the concatenated string
Example:
4.4 indexOf(value, |startPosition): Find a string or character from front to back in the instance and return the found position (counting from 0). If not found, return -1
Parameters:
①value {string}: Search string
②startPosition {int} Optional: the starting position to start searching, the default is to start searching from position 0
Return value:
{int} Returns the found position (counting from 0). If not found, return -1
Example:
4.5 lastIndexOf(value, |startPosition): Search for a string or character from back to front in the instance and return the found position (counting from 0). If not found, return -1
Parameters:
①value {string}: Search string
②startPosition {int} Optional: the starting position to start searching, the default is to start searching from the end
Return value:
{int} Returns the found position (counting from 0). If not found, return -1
Example:
4.6 localeCompare(value): Compare the instance with the parameter and return the comparison result
Parameters:
①value {string}: The string to be compared
Return value:
0: The instance is larger than the parameter
1: Instance and parameter are equal
-1: The instance is smaller than the parameter
Example:
4.7 match(regexp): Use regular expressions for matching search
Parameters:
①regexp {regexp}: regular expression, eg: /d /
Return value:
According to whether the regular expression has the attribute 'g', different results are returned; if there is no match, {null} is returned:
①The regular expression does not have the attribute 'g', performs a match, and returns a {single match} result object. The object contains the following attributes:
Array serial number: indicates the matching result, 0 is the matching text, 1 is the matching result of the first parenthesis from the right to the right, 2 is the second parentheses, and so on
Index attribute: indicates that the matching text is at the starting position of the matching source
Input attribute: indicates the matching source
②The regular expression has the attribute 'g', performs global matching, finds all matching objects in the string, and returns a {string array}: the array element contains each matching object in the string, excluding the regular expression in parentheses The string also does not provide index and input attributes.
Example:
4.8 replace(regexp, replaceStr): Replace the substring matched by the regular expression and return the replaced string
Parameters:
①regexp {regexp}: Regular expression. eg:/d/
②replaceStr {string | function}:
1) If it is a string, it means the replacement string, and all matched strings will be replaced with this string;
The $ character in a string has special meaning:
$1,$2 ... $99: Indicates the matching sub-items of ①parameters from left to right parentheses
$&: Indicates the sub-items matched by the entire ① parameter
$$: dollar sign
2) If it is a function, it means that this function is called for each matching result. The only parameter of the function is the matching result, and a replacement result is returned.
Return value:
{string} returns a replaced string
Example:
4.9 search(regexp): Returns the position where the first match of the regular expression is found
Parameters:
①regexp {regexp}: Regular expression. eg:/d/
Return value:
{int} Returns the position of the first matching result; if no matching result is found, -1 is returned
Example:
4.10 slice(start, |end): Returns the substring from the start position of the string to the position before end
Parameters:
①start {int}: The starting position index of substring extraction (including the character at this position).
If the number is negative, it means counting from the end of the string. For example: -1 represents the last string, -2 represents the second to last character.
②end {int} Optional: The end position index of substring extraction (excluding the character at this position).
If the number is negative, it means counting from the end of the string. For example: -1 represents the last string, -2 represents the second to last character.
If this parameter is omitted, all characters from the start position to the end will be returned.
Note:
The order of extracting substrings is from left to end. If the start index position is greater than the end index position, an empty string will be returned.
Return value:
{string} returns the substring from the start position of the string to the position before the end.
Example:
4.11 split(delimiter, |arrayLength): Split the string into an array of strings according to a certain delimiter and return
Parameters:
①delimiter {regexp | string}: The specified delimiter, which can be a regular expression or a string.
②arrayLength {int} Optional: The length of the split array. If omitted, all split substrings are returned.
Note:
If the delimiter is at the first or last character of the string, an empty string will be added to the returned array.
Return value:
{ string[] } Returns an array consisting of strings.
Example:
4.12 substr(start, |wordLength): Returns a substring starting from the start position of the string to wordLength lengths
Parameters:
①start {int}: The starting position index of substring extraction (including the character at this position).
If the number is negative, it means counting from the end of the string. For example: -1 represents the last string, -2 represents the second to last character.
②wordLength {int} Optional: Extract the length of characters. If this parameter is omitted, all characters from the start position to the end are returned.
Return value:
{string} returns the extracted string
Example:
4.13 substring(start, |end): Returns the substring from the start position of the string to the position before end
Parameters:
①start {int}: The starting position index of substring extraction (including the character at this position). The number cannot be negative. If it is a negative number, it will be processed as 0
②end {int} Optional: The end position index of substring extraction (excluding the character at this position). The number cannot be negative. If it is a negative number, it will be processed as 0
Return value:
{string} returns the substring from the start position of the string to the position before the end.
Example:
4.14 toUpperCase(): Convert the string to uppercase and return
4.15 toUpperCase(): Convert the string to lowercase and return
4.16 trim(): remove whitespace characters at the beginning and end of the string and return
The above is the entire content of this article. I hope that through this article, everyone can have a new understanding of the String object in JavaScript.