An overview,

The String object is one of three wrapper objects that JavaScript natively provides to generate String objects.

In the above code, the string object corresponding to the string ABC has a numeric key (0, 1, 2) and a length attribute, so it can be evaluated like an array.

The above code converts the Boolean value ture and the value 5 to strings, respectively.

Second, static method

2.1 String. FromCharCode ()

Static methods: methods defined in the object itself, rather than in the object instance

The main static methods provided by strings are string.fromCharcode (), which takes one or more numeric values representing Unicode code points and returns a String of those code points.

Note that this method does not support characters with Unicode code points greater than 0xFFFF, meaning that the parameter passed cannot be greater than 0xFFFF (65535 in decimal).

In the above code, the String. FromCharCode argument 0x20BB7 is greater than 0xFFFF, which results in an error. The character 0x20BB7 corresponds to the Chinese character 𠮷, but the result is another character (code point 0x0BB7). This is because string.fromCharcode finds that the parameter value is greater than 0xFFFF and ignores the extra bits (that is, the 2 in 0x20BB7).

The root cause of this phenomenon is that characters with code points greater than 0xFFFF take up four bytes, whereas JavaScript supports two bytes by default. In this case, 0x20BB7 must be split into two characters.

In the code above, 0x20BB7 is split into two characters 0xD842 and 0xDFB7 (that is, two two-byte characters combined into a four-byte character) to get the correct result. The four-byte representation of characters with code points greater than 0xFFFF is determined by the UTF-16 encoding method.

Instance attributes

3.1 the String. The prototype. Length

4. Instance method

4.1 the String. The prototype. CharAt ()

The charAt method returns a character at the specified position, numbered from 0.

This method can be replaced by array subscripts.

If the argument is negative, or greater than or equal to the length of the string, charAt returns an empty string.

4.2 the String. The prototype. CharCodeAt ()

The charCodeAt method returns the Unicode code point (in decimal notation) at the specified position in the String, equivalent to the inverse of String.fromCharcode ()

If there are no arguments, charCodeAt returns the Unicode code point for the first character.

If the argument is negative, or greater than or equal to the length of the string, charCodeAt returns NaN.

Note that the charCodeAt method returns no more than 65536 Unicode code points (0xFFFF), that is, only two bytes of code points. If the code point is more than 65536 characters (four byte characters), it is necessary to use charCodeAt twice in succession, not only read charCodeAt(I), but also read charCodeAt(I +1), put the two values together to get the exact character.

4.3 the String. The prototype. The concat ()

The concat method is used to concatenate two strings, returning a new string without changing the original string.

This method can accept multiple parameters.

In the above code, the concat method converts the argument to a string and then concatenates it, so it returns a three-character string. By contrast, the plus operator does not convert when both operands are numeric, so it returns a two-character string.

4.4 the String. The prototype. The slice ()

The slice method is used to fetch substrings from the original string and return them without changing the original string. Its first argument is the start of the substring, and its 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.

If the first argument is greater than the second, the slice method returns an empty string.

4.5 the String. The prototype. The substring ()

The substring method is used to retrieve substrings from the original string and return them without changing the original string, much like the slice method. The first argument represents the start of the substring and the second position represents the end position (which is not included in the return result).

If the first argument is larger than the second, the substring method automatically changes the position of the two arguments.

In the code above, swapping the two arguments to the subString method yields the same result.

If the argument is negative, the substring method automatically converts the negative number to 0.

In the code above, the argument -3 in the second example will automatically become 0, equivalent to ‘JavaScript’.substring(4, 0). Since the second argument is less than the first, the positions are automatically swapped, so Java is returned.

Since these rules are counterintuitive, the substring method is not recommended in favor of slice.

4.6 String. The prototype. The substr ()

The substr method retrieves and returns a substring from the original string, unchanged, in the same way that the slice and substring methods do.

The first argument to the substr method is the starting position of the substring (evaluated from 0), 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.

In the code above, the argument -1 in the second example is automatically converted to 0, indicating that the substring length is 0, so an empty string is returned.

4.7 the String. The prototype. IndexOf (), String. The prototype. The lastIndexOf ()

The indexOf method is used to determine the position of the first occurrence of a string in another string, returning the position at which the match began. If -1 is returned, there is a mismatch.

The lastIndexOf method is used in the same way as indexOf, except that lastIndexOf matches from the tail and indexOf matches from the head.

The indexOf method can also accept a second argument that indicates a backward match from that location.

The second argument to lastIndexOf represents a forward match from that position.

The trim method removes whitespace from both ends of the string and returns a new string, unchanged.

4.9 the String. The prototype. ToLowerCase (), String. The prototype. The toUpperCase ()

The toLowerCase method is used to convert all of a string toLowerCase, and toUpperCase is used to convert all of a string toUpperCase. They both return a new string, leaving the original string unchanged.

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

The array is also returned with the index and input attributes, representing the start of the matching string and the original string, respectively.

The search method is used basically the same as match, but returns the first position of the match. If no match is found, -1 is returned.

The replace method replaces the substring of a match, usually only the first match (unless a regular expression with a G modifier is used).

The split method splits the string according to the given rules and returns an array of split substrings.

If the split rule is an empty string, the members of the array are returned as each character of the original string.

If the argument is omitted, the only member of the array returned is the original string.

If the two parts that satisfy the split rule are next to each other (that is, there are no other characters between the two splits), an empty string will be returned in the array.

If the part that satisfies the split rule is at the beginning or end of the string (that is, it is preceded or followed by no other characters), the first or last member of the array is returned as an empty string.

The split method can also take a second argument that limits the maximum number of members returned from the array.

In the following code, the second argument to the split method determines the number of members to return.

The localeCompare method is used to 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.

The most important feature of this method is that it considers the order of natural language. For example, uppercase letters are normally smaller than lowercase letters.

In the following code, letter B is less than letter A. Because JavaScript uses Unicode code point comparisons, B’s code point is 66 and A’s code point is 97.

However, the localeCompare method takes into account the natural language ordering and places B before A.

In the following code, the localeCompare method returns the integer 1, indicating that B is larger.

LocaleCompare can also have a second parameter that specifies the language used (English by default), and then compare against the rules of that language.

This article reprinted from http://javascript.ruanyifeng.com/stdlib/string.html