JavaScript native Object properties and methods detailed--string object

Source: Internet
Author: User
Tags deprecated

length

The Length property can return the number of characters in a string.

Length is obtained based on the UTF-16 encoding of the string, with an empty string length of 0. Length cannot be modified.

charAt ()

The CharAt () method returns the character at the specified position. Note that JavaScript does not have a character data type that differs from the string type, so the returned character is a string of length 1 .

Stringobject.charat (Index)

The parameter index is required. A number that represents a position in a string, which is the subscript of a character in a string. The subscript for the first character in a string is 0. If the parameter index is not between 0 and String.Length, the method returns an empty string.

Note: the CharAt () method is problematic for some non-BMP (Basic-multilingual-plane) character support, refer to: MDN

charCodeAt ()

The charCodeAt () method returns the Unicode encoding of the character at the specified position. This return value is an integer between 0–65535.
Method charCodeAt () is similar to the operation performed by the CharAt () method, except that the former returns the encoding of the character at the specified position, and the latter returns a character string.

Stringobject.charcodeat (Index)

The parameter index is optional. A number that represents a position in a string, which is the subscript of a character in a string. The subscript for the first character in a string is 0. If index is a negative number, or is greater than or equal to the length of the string, charCodeAt () returns NaN. The default is 0 when index is empty.

The range of Unicode encoding is 0 to 1,114,111. The first 128 Unicode encoding matches the ASCII character encoding. The value returned by the charCodeAt () method is always less than 65536, because the character Fu Huicheng of the higher value appears and needs to be retrieved simultaneously with charCodeAt (i) and charCodeAt (i+1).

concat () – Not recommended

The Concat () method is used to concatenate two or more strings.

Stringobject.concat (STRINGX, Stringx, ..., STRINGX)

The parameter stringx is required . is one or more string objects that will be concatenated as a string.

The concat () method converts all its arguments to a string, then connects sequentially to the end of the string stringobject and returns the concatenated string. Please note that theStringobject itself has not been changed.

Note that it is strongly recommended to use the "+" operator to concatenate strings, instead of this method, and more efficient, refer to: concat vs + vs Join.

indexOf ()

The IndexOf () method returns the position of the first occurrence of a specified string value in a string.

Stringobject.indexof (Searchvalue, Fromindex)

The parameter searchvalue is required , which specifies the string value to be retrieved. The parameter fromindex is an optional integer parameter. Specifies where to start retrieving in the string. Its legal value is 0 to stringobject.length–1. If this argument is omitted, it is retrieved from the first character of the string.

The method will retrieve the string stringobject from beginning to end to see if it contains substring searchvalue. The location at which to begin retrieving is at the fromindex of the string or at the beginning of the string (when no fromindex is specified). If a searchvalue is found, the position of the first occurrence of the Searchvalue is returned. The character position in Stringobject is starting at 0.

Note: the IndexOf () method is case-sensitive! If the string value that you want to retrieve does not appear, the method returns-1.

lastIndexOf ()

The LastIndexOf () method returns the location of the last occurrence of a specified string value, which is searched from behind at the specified position in a string.

The LastIndexOf () and indexOf () parameters are consistent with the use method, except that they are searched backward.

var str= "Hello World world!" Console.log (Str.indexof ("Wo"));        //6console.log (Str.indexof ("Wo", 2));      /         /////////6

Localecompare ()

Compares two strings in a local-specific order.

Stringobject.localecompare (target)

The parameter target is a required string to be compared with stringobject in a local-specific order.

Returns the number of the comparison result. If Stringobject is less than target, Localecompare () returns a number less than 0. If Stringobject is greater than target, the method returns a number greater than 0. If two strings are equal, or if there is no difference based on the local collation, the method returns 0.

When you apply the < and > operators to Strings, they compare strings with only Unicode encoding of characters, regardless of local collation. The order generated in this way is not necessarily correct. For example, in Spanish, where the character "ch" is usually ordered as a character that appears between the letters "C" and "D". The Localecompare () method provides a way to compare strings, taking into account the default local collation.

Localecompare () parameters in some advanced browsers also support locales and options, refer to the following code and MDN:

different cultures have different sorting rules (' Console.log '. Localecompare (' z ', ' de '));  //-1 console.log (' Up ') localecompare (' Z ', ' SV '));  //1    

match ()

The match () method retrieves the specified value within a string, or finds a match for one or more regular expressions.

The method is similar to IndexOf () and lastIndexOf (), but it returns the specified value instead of the position of the string.

Stringobject.match (RegExp)

The parameter RegExp can be a string, or it can be a regular expression RegExp object.

Returns an array that holds the matching results. The contents of the array depend on whether the regexp has global flag G.

If RegExp does not have a flag G, then the match () method can only perform a match in Stringobject. If no matching text is found, match () returns NULL. Otherwise, it returns an array that holds information about the matching text it finds. The No. 0 element of the array holds the matching text, while the rest of the elements hold the text that matches the subexpression of the regular expression. In addition to these regular array elements, the returned array contains two object properties . The Index property declares the position of the starting character of the matching text in Stringobject, and the input property declares a reference to Stringobject.

If RegExp has the flag G, the match () method performs a global retrieval and finds all matching substrings in the stringobject. If no matching substring is found, NULL is returned. If one or more matching substrings are found, an array is returned. However, the contents of the array returned by the global match are very different from the former, and its array elements hold all the matching substrings in the Stringobject, and There is no index attribute or input property .

Without the flag G, the result of calling Stringobject.match (RegExp) and calling Regexp.exec (Stringobject) is the same. In global retrieval mode, match () does not provide information about the text that matches the subexpression, nor does it declare the location of each matched substring. If you need these globally retrieved information, you can use Regexp.exec ().

Note: If you need to know if a string matches a regular expression, use Regexp.test (string), or use Regexp.exec (string) instead of String.match (regexp) If you want to match only one time.

var str= "Hello world!" var str2= "1 plus 2 equal 3"Console.log (Str.match ("World"));  //["World", Index:6, Input: "Hello world!"] Console.log (Str2.match (/\d+/g));  //["1", "2", "3"]     

replace ()

The replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.

Stringobject.replace (REGEXP/SUBSTR, replacement)

The parameter regexp/substr is required . A RegExp object that specifies the substring or pattern to replace. If the value is a string, it is used as the direct volume text pattern to be retrieved, instead of being converted to the RegExp object first. The parameter replacement is required . is a string value. A function that specifies replacement text or generates alternate text .

The replacement method returns a new string that is replaced with the first match of RegExp or all of the matches.

The replace () method of the string Stringobject performs a find-and-replace operation. It looks for substrings in Stringobject that match regexp, and then replaces them with replacement. If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matched substring.

Replacement can be a string, or it can be a function. If it is a string, then each match is replaced by a string. However, the $ character in replacement has a specific meaning. As shown below, it shows that the resulting string from pattern matching will be used for substitution:

    • $$–$
    • $ '-text that is located to the left of the matching substring.
    • $ '-text that is located to the right of the matching substring.
    • $&-a substring that matches the regexp.
    • $number-text that matches the number sub-expression in RegExp.

Replacement can be a function, in which case each match calls the function, and the string it returns is used as the replacement text. The first parameter of the function is a string that matches the pattern. The next parameter is a string that matches the subexpression in the pattern and can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. The last parameter is the stringobject itself.

//Replace oncevar str = "Hello microsoft!"; Console.log (Str.replace (/microsoft/, "Google"));//Hello Google!console.log (str);//Hello microsoft!//Replace multiple timesvar str2 = "Hello microsoft! and microsoft! and microsoft! or microsoft! "; Console.log (Str2.replace (/microsoft/g, "Google"));//Hello google! and google! and google! or google!//Character conversionsvar str3 = "Doe, John" ;console.log (Str3.replace (/(\w+) \s*, \s* (\w+)/, "$ $ $")); //john doevar STR4 = ' "A "," B "' ;console.log (Str4.replace ([^"]*) "/g," ' $ ' "); //// using the function var STR5 = ' aaa bbb CCC ' 

Search ()

The search () method is used to retrieve the substring specified in a string, or to retrieve a substring that matches a regular expression.

Stringobject.search (RegExp)

The parameter RegExp can be a substring that needs to be retrieved in Stringobject, or it can be a RegExp object that needs to be retrieved.

Returns the starting position of the first substring in stringobject that matches the regexp. Returns 1 if no matching substring is found.

Note: The search () method does not perform a global match , and it ignores the flag G. It ignores the LastIndex property of RegExp and always retrieves from the beginning of the string, which means that it always returns the first matching position of the stringobject.

var str = "Hello microsoft!" ; Console.log (Str.search (/microsoft/));   //6  

If you just want to know if there is a matching string, use Search () and use the test () method almost. If you want to get more information, you can use the match () and the Exec () method, but the efficiency will be low.

Slice ()

The slice () method extracts a portion of a string and returns the extracted part with a new string.

Stringobject.slice (start, end)

The parameter start is the starting subscript for the fragment to be extracted. If it is a negative number, the parameter specifies where to start from the end of the string. That is,-1 refers to the last character of the string, 2 refers to the second-lowest character, and so on.

The parameter end is the subscript immediately following the end of the fragment to be extracted. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If the parameter is negative, it specifies the position from the end of the string.

method returns a new string. Includes the string Stringobject all characters from start ( including start) to end ( not including end).

Note: The method slice (), substring (), and substr () of the string object can return the specified part of the string. It is strongly recommended to use the slice () method on all occasions.

var str = "Hello microsoft!" ; Console.log (Str.slice (6));      //Microsoft!console.log (Str.slice (6));  //Micros    

substring ()

Deprecated, it is recommended to use slice () instead.

substr ()

Deprecated, it is recommended to use slice () instead.

toLocaleLowerCase ()

Deprecated and only useful in a few languages, such as Turkish, toLowerCase () is recommended instead.

toLocaleUpperCase ()

Deprecated and only useful in a few languages, such as Turkish, toUpperCase () is recommended instead.

toLowerCase ()

The toLowerCase () method is used to convert a string to lowercase.

toUpperCase ()

The toUpperCase () method is used to convert a string to uppercase.

The string object also has many methods for HTML tags: anchor (), Big (), Blink (), bold (), fixed (), FontColor (), FontSize (), italics (), link (), Small (), Strike (), sub (), SUP (). They are mainly HTML format for string objects, and now few people will be applied to it, not recommended . See also: Demo code

JavaScript native Object properties and methods detailed--string object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.