JavaScript string Common operation Summary, JS string Operation Daquan

Source: Internet
Author: User

JavaScript string Common operations Summary, JS String Operations Daquan C # string and regular expression reference manual PDF ebookDownload Now

The manipulation of strings is very frequent and important in JS. In the past after reading the book can be remembered very clearly, but a little time without, will forget almost, bad memory is flawed ah ... Today on the string of some common operations to do a collation, accesses than either deepen the impression, both easy to review in the future.

String Object Properties

(1) Length Property

Length is a very common attribute in a string, and its function is to get the length of the string. Of course, it is important to note that the Chinese characters in JS are only one character, which may be different from other languages.

var str = ' abc '; Console.log (str.length);

(2) Prototype property

Prototype is often used in object-oriented programming to add properties or methods to an object, and to add methods or properties that are shared across all instances. Therefore, it is often used to extend the JS built-in objects, such as the following code to add a string to remove both sides of the method:

String.prototype.trim = function () {    return this.replace (/^\s*|\s*$/g, ');}

String Object method

1. Get the class method

(1) charAt ()

Stringobject.charat (Index)

The CharAt () method can be used to get a string at the specified position, index is a string-indexed value, starting from 0 to string.leng–1, and an empty string is returned if not in this range. Such as:

var str = ' ABCDE '; Console.log (Str.charat (2));     Return to Cconsole.log (Str.charat (8));     Returns an empty string

(2) charCodeAt ()

Stringobject.charcodeat (Index)

The charCodeAt () method returns the Unicode encoding of the character at the specified position. The charCodeAt () method, like the Charat () method, requires passing an index value as a parameter, which is the encoding of the character that returns the specified position, while the latter returns a character string.

var str = ' ABCDE '; Console.log (str.charcodeat (0));     Returns 97

(3) fromCharCode ()

String.fromCharCode (numx,numx,..., numx)

fromCharCode () can accept one or more Unicode values and then return a string. In addition, the method is a static method of string, and each character in the string is specified by a separate numeric Unicode encoding.

String.fromCharCode (98, 101)   //Return ABCDE

2. Find the class method

(1) indexOf ()

Stringobject.indexof (Searchvalue,fromindex)

IndexOf () is used to retrieve the position of the specified string value in the first occurrence of the string. It can receive two parameters, Searchvalue represents the substring to find, Fromindex represents the start of the lookup, and if omitted, it is retrieved from the starting position.

var str = ' ABCDEABCDE '; Console.log (Str.indexof (' a '));  Return 0console.log (Str.indexof (' A ', 3));   Returns 5console.log (str.indexof (' BC ')); Returns 1

(2) LastIndexOf () method

Stringobject.lastindexof (Searchvalue,fromindex)

The lastIndexOf () syntax is similar to indexof (), which returns the position of the last occurrence of a specified substring value, which is retrieved from the backward forward.

var str = ' ABCDEABCDE '; Console.log (Str.lastindexof (' a '));  Return 5console.log (Str.lastindexof (' A ', 3));   Returns 0 retrieves Console.log (str.lastindexof (' BC ') from the position of index 3 forward; Returns 6

(3) Search () method (substr) (regexp)

The search () method is used to retrieve the substring specified in a string, or to retrieve a substring that matches a regular expression. It returns the starting position of the first matched substring, or 1 if there is no match.

var str = ' AbcDEF '; Console.log ( (' C '));   Returns 2console.log ( (' d '));   Return to -1console.log ( (/d/i));  Returns 3

(4) Match () method

Stringobject.match (substr) stringobject.match (regexp)

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

If a substring is passed into the argument or a regular expression is not globally matched, the match () method performs a match from the start position and returns null if there is no match to the result. Otherwise, an array is returned, the No. 0 element of the array holds the matching text, and in addition, the returned array contains two object attributes index and input, respectively, representing the starting character index of the matched text and the Stringobject reference (that is, the original string).

var str = ' 1a2b3c4d5e '; Console.log (Str.match (' h '));    Returns Nullconsole.log (Str.match (' B '));    returns ["B", Index:3, Input: "1a2b3c4d5e"]console.log (Str.match (/b/));    return ["B", Index:3, Input: "1a2b3c4d5e"]

If the parameter passes in a regular expression with a global match, match () matches () multiple times from the start position until the end. If there is no match to the result, NULL is returned. Otherwise, an array is returned, with all the required substrings in the array, and no index and input attributes.

var str = ' 1a2b3c4d5e '; Console.log (Str.match (/h/g));   Return to Nullconsole.log (Str.match (/\d/g));  return ["1", "2", "3", "4", "5"]

3. Intercepting class methods

(1) substring ()

Stringobject.substring (Start,end)

SUBSTRING () is the most commonly used method of string interception, which can receive two parameters (parameters cannot be negative), the start and end positions to intercept, respectively, and it will return a new string with the contents of all characters from start to end-1. If the end parameter (end) is omitted, it is truncated from the start position to the last.

var str = ' ABCDEFG '; Console.log (str.substring (1, 4));   Return to Bcdconsole.log (str.substring (1));  Return to Bcdefgconsole.log (str.substring (-1)); Returns ABCDEFG, which is treated as 0 when a negative value is passed in

(2) Slice ()

Stringobject.slice (Start,end)

The slice () method is very similar to the substring () method, and its incoming two parameters correspond to the starting and ending positions, respectively. The difference is that the argument in slice () can be negative, and if the argument is negative, the parameter specifies the position from the end of the string. That is, 1 refers to the last character of the string.

var str = ' ABCDEFG '; Console.log (Str.slice (1, 4));   Return to Bcdconsole.log (Str.slice (-3,-1)); Return to Efconsole.log (Str.slice (1,-1));  Return to Bcdefconsole.log (Str.slice (-1,-3)); Returns an empty string, or null if there is a problem with the parameter passed in.

(3) substr ()

Stringobject.substr (Start,length)

The substr () method extracts a specified number of characters from the start subscript in a string. The return value is a string that contains the length character starting at start of Stringobject, including the character that the start refers to. If length is not specified, the returned string contains the character from start to the end of Stringobject. In addition, if start is a negative number, it is counted from the end of the string.

var str = ' ABCDEFG '; Console.log (Str.substr (1, 3)   //Return Bcdconsole.log (STR.SUBSTR (2))  //Return to Cdefgconsole.log ( Str.substr ( -2, 4))  //return FG with a larger target length, whichever is the actual intercept length

4. Other methods

(1) Replace () method

Stringobject.replace (regexp/substr,replacement)

The replace () method is used for string substitution operations, which can receive two parameters, the former being the substituted substring (which can be regular) and the text to replace.

If the first parameter passes in a substring or a regular expression that does not have a global match, then the replace () method will only make one replacement (that is, replace the front one), returning the result string after the substitution.

var str = ' ABCDEABCDE '; Console.log (Str.replace (' A ', ' a ')); Console.log (Str.replace (/a/, ' a '));

If the first parameter passes in a globally matched regular expression, replace () replaces the qualifying substring multiple times, and finally returns the result string that has been replaced several times.

var str = ' ABCDEABCDEABCDE '; Console.log (Str.replace (/a/g, ' a '));    Returns AbcdeAbcdeABCDEconsole.log (Str.replace (/a/gi, ' $ '));   Back to $BCDE$BCDE$BCDE

(2) Split () method

Stringobject.split (Separator,howmany)

The split () method is used to split a string into an array of strings. The first parameter, separator, represents the split position (the reference character), and the second parameter, Howmany, represents the maximum allowable length of the returned array (not normally set).

var str = ' a|b|c|d|e '; Console.log (str.split (' | '));    returns ["A", "B", "C", "D", "E"]console.log (Str.split (' | ', 3)); returns ["A", "B", "C"]console.log (Str.split (')); returns ["a", "|", "b", "|", "C", "|", "D", "|", "e"]

You can also use regular to split

var str = ' a1b2c3d4e '; Console.log (Str.split (/\d/)); returns ["A", "B", "C", "D", "E"]

(3) toLowerCase () and toUpperCase ()

Stringobject.tolowercase () stringobject.touppercase ()

The toLowerCase () method converts uppercase letters in a string to lowercase, and the toUpperCase () method converts lowercase letters in a string to uppercase.

var str = ' JavaScript '; Console.log (Str.tolowercase ()); Return to Javascriptconsole.log (Str.touppercase ()); Back to JavaScript

JavaScript string Common operation Summary, JS string Operation Daquan

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: 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.