A summary of common methods for JS string objects

Source: Internet
Author: User
Tags what array

String objects are commonly used to hold data in the form of text.

There are two methods of conversion:

String (s) new string (s)
The string object methods are:CharAt () charCodeAt () concat () indexOf () lastIndexOf () match () repeat () replace () search () slice () substr () substring () Split () toLowerCase () toUpperCase () trim () valueOf () toString ()

One, CharAt () method

The method returns the character at the specified position.

Grammar:

String.charat (Index)

Index represents an integer between 0 and String.length-1. If no index is provided, 0 is used by default.

Example:

var str= "Life is cool! Come on ";d ocument.write (Str.charat (2)); // F

Second, charCodeAt () method

The method returns a Unicode encoded value that represents the character at the given index, typically ranging from 0 to 65535, or if the index is out of range NaN .

Example:

var str= "abc";d ocument.write (Str.charcodeat (0)); //  the

Third, concat () method

This method is used to stitch multiple strings, and the performance of the "+" operator is not good. It is recommended that you use the "+" operator more.

Example:

var str= "abc";d ocument.write (Str.concat ("DEFG", "!")); // abcdefg!

Iv. indexOf ()

This method finds the index of the position where the specified value first appears. Returns 1 if not found. The method is case-sensitive.

Grammar:

IndexOf (Str,fromindex)

STR: Indicates the value to find;

FromIndex: Indicates where to start the lookup, which defaults to 0.

Example:

var str= "Life was cool";d ocument.write (Str.indexof ("is"); // 5!

Detects if a character can be used and the "~" operator to determine if it is present.

Wu, LastIndexOf ()

Returns the location of the last occurrence of a specified string value, followed by a search in a string at the specified position. Returns 1 if not found.

Grammar:

Str.lastindexof (search, index)

An optional index indicates the location of the lookup. The default is the length of the string, which is the search from the end.

 例子:

var str= "Life was Life";d ocument.write (Str.lastindexof ("F", 5)); //2 The  last F is removed, so the F of index 2 becomes the last F of the string .

VI. Match () method

Find the item that matches the regular match.

Grammar:

Str.match (RegExp);

The regexp represents a regular object. If a non-regular expression object is passed in, it is implicitly used to new RegExp() convert it to a regexp.

The return value is an array of results that hold one or more matches (depending on the regular G-flag).

Example:

var str= "6 life is cool 562"; document.write (Str.match (/\d/g)); //6,5,6,2

Vii. Repeat () method

The method represents the number of times the string repeats

Grammar:

Str.repeat (count)

Count indicates the number of repetitions. An integer between 0 and positive infinity

Example:

var str= "Life";d ocument.write (Str.repeat (2)); // Lifelife

Eight, replace () method

Call this method to replace some characters with another character, or to replace a substring that matches a regular expression.

Grammar:

Str.replace (regexp/substr,replacement|function)

Parameter 1 means: a regular object or a simple string (the string is considered a whole, and only the first match is replaced);

A parameter of 2 means to replace the matching string in Parameter 1, or to replace the match in Parameter 1 with the return value of a function.

The return value is a new string that partially or completely matches the substitution pattern.

Example:

var str= "Life was cool life is Beautiful";d ocument.write (str.replace (/life/g, "web")); //
//Swap two words in a string var str= "Life is";d ocument.write (Str.replace (/(\w+) \s (\w+)/, "$ $")); //

Ix. Search () method

The method retrieves a substring that matches the regular expression. If a non-regular expression object is passed in, it is implicitly converted to a regular expression object by using new RegExp (obj).

The return value is the first-match index in the matched string. No then returns-1.

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

Example:

var str= "Life is Cool";d ocument.write (Str.search ("E")); // 3

Ten, Slice () method

This method is used to extract part of a string and to return a new string.

Grammar:

Str.slice (Start,end)

The start parameter represents the starting subscript for the extraction, and if it is a negative number, the length of the string is then started to extract.

The end parameter indicates that the fetch string ends at that index, and if omitted, the parameter is extracted to the end of the string. If a negative number is added, the length of the string is then extracted.

This method is similar to the slice function of an array. The extracted string index includes start, but does not include end.

Example:

var str= "Life is Cool";d ocument.write (Str.slice (2)); //fe is cool
var str= "Life is Cool";d ocument.write (Str.slice (-8,-2)); //

Xi. substr () method

The substr () method extracts a specified number of characters from the start subscript in a string.

Grammar:

SUBSTR (Start,length)

The start parameter represents the starting index of the fetch. Negative words plus string lengths to extract.

The length parameter represents the number of characters extracted, and the ellipsis represents the extract to the end.

Example:

var str= "Life is Cool";d ocument.write (str.substr (2,5)); //

12. Substring () method

The method is used to extract the characters between two specified subscripts in a string.

Grammar:

Str.substring (Start,stop)

The return value is a new string whose contents are all characters from start to stop-1.

The substring () method parameter does not accept negative numbers, which are different from the two methods of slice and substr.

Example:

var str= "Life is Cool";d ocument.write (str.substring (2,8)); //

13. Split () method

This method is used to split a string into an array of strings.

Grammar:

Str.split ([separator,[length])

The Separtor parameter represents the character to be split, which can be a string or a regular expression, and if omitted, returns the array form of the entire string;

If this argument is an empty string, each character in the original string is returned as an array. The action performed by String.Split () is the opposite of what Array.join does.

The length parameter represents the lengths returned by the array. If the entire string is omitted, it will be split.

Example:

//without parameters this entire string is returned as an array var str= "Life is Cool";d ocument.write (Str.split ()); //
//parameter is a null character, each character of the string is split var str= "Life is Cool"; document.write (Str.split ("")); //
var str= "Life is Cool";d ocument.write (Str.split ("", 4)); // l,i,f,e
var str= "Life7is8cool9";d ocument.write (str.split (/\d/)); //

14. toLowerCase () method

This method converts the string to lowercase.

Example:

var str= "Life is COOL"; document.write (Str.tolowercase ()); //life is cool
XV, toUpperCase ()

The method converts the string value to uppercase and returns.

Example:

var str= "Life is Cool";d ocument.write (Str.touppercase ()); //

16. Trim () method

This method is used to remove whitespace at both ends of a string. Returning a new string does not affect the original string. (Support IE9 above)

17. ValueOf () method

The method returns String the original value of an object. This method is usually called inside JavaScript, not in code. Generally used for unpacking.

18. ToString () method

The wonderful thing is that besides arrays, booleans, numbers, and so on have the ToString method, the string also has, but generally does not call the method.

A summary of common methods for JS string objects

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.