A detailed description of JavaScript string processing

Source: Internet
Author: User
Tags first string

First, create a string

Create a string, wrap a set of strings in quotation marks, and assign them to a string variable.

var  Jsstr= "Hello,javascript string!";

Second, string lookup method

1. Character Method Charat (), charCodeAt (), fromCharCode ()

(1) charAt () function

Function: Returns the character of the specified position in the string;

Syntax: String.charat (n);

Arguments: The position of the n--character in the string (the position of the first character of the string is 0);

Return value: The character that returns n position, if n is not between 0 and (string.length-1), returns an empty string.

Example:

(2) charCodeAt () function

Function: Returns the Unicode encoding of the character at the specified position;

Syntax: String.charcodeat (n);

Arguments: The position of the n--character in the string (the position of the first character of the string is 0);

Return value: Returns the Unicode encoding of the n position (this is 16 bits, between 0-65536), and if n is not between 0 and (String.length-1), Nan is returned.

Example:

(3) fromCharCode () function

Function: Accepts the specified Unicode value and returns a string;

Syntax: String.fromCharCode (numx,numx ...);

Parameters: numx--must be a value, one or more Unicode values, through the fromCharCode function to get the Unicode worthy string;

return value: string;

Attribute: A static method, which is actually a property of the constructor string ();

Example:

Summary:

There is a commonality between charAt () and charCodeAt (), whose function is to find the specified character according to the subscript, the parameter n is the word Poute to be queried, the valid range is between 0~jsstr.length-1; charCodeAt () and fromCharCode () Each other is a reverse operation.

2. Location method indexof (), LastIndexOf ()

(1) indexOf () function

Function: Retrieves the position of the first occurrence of the specified string in the string;

Syntax: String.IndexOf (SEARCHVALUE,FROMINDEX);

Parameters: searchvalue--required, substring to find;

fromindex--an optional integer parameter. The legal value range is 0 to string.length-1, negative value defaults to 0, and if omitted, this parameter will be searched by default from the beginning of the string;

Return value: Returns the position of the substring searchvalue in the first occurrence of the string sring, without a substring in string searchvalue, returning-1;

Example:

(2) lastIndexOf () function

Function: Retrieves the position of the last occurrence (match) of the specified string in the string;

Syntax: String.LastIndexOf (SEARCHVALUE,FROMINDEX);

Parameters: searchvalue--required, substring to find;

fromindex--an optional integer parameter. The legal value range is 0 to string.length-1, negative value defaults to 0, and if omitted, this parameter will be searched by default from the last position of the string;

Return value: Returns the substring searchvalue at the last occurrence in the string sring, without a substring in string searchvalue, returning-1;

Example:

Summary:

The IndexOf () and LastIndexOf () functions are common in that the function is to find the subscript of the specified substring in the string, with the same parameter requirements, to return the first occurrence of the subscript when the substring is in the string, or 1.

3. Matching method match (), search (), replace (), split ()

(1) Match () function

Function: Retrieves the value specified in the string and finds the match of one or more regular expressions;

Syntax: String.match (Searchvalue);

String.match (RegExp);

Parameter: searchvalue--required, string value to retrieve;

regexp--required. The RegExp object to match;

Return value: The array that holds the matching result, the content of the array depends on whether the regexp has global variable g;

Example:

(2) Search () function

Function: Retrieves the value specified in a string, or retrieves a substring that matches a regular expression;

Syntax:string.search (regexp);

Parameter: regexp--required. The parameter can be a substring that needs to be retrieved in a string, or it can be a RegExp object that needs to be retrieved;

Return value: The starting position of the first substring in a string string that matches regexp, or 1 if no match is reached;

Example:

(3) Replace () function

Function: Used to find a string and replace it with another string;

Syntax: String.Replace (regexp/substr,repalcement);

Parameter: regexp/substr--required. Specifies the substring or RegExp object to replace

repalcement--is optional. A string that specifies the replacement text or function that generates the replacement text;

Return value: A new string that is replaced with the resulting.

Example:

(4) Split () function

Function: Divides the string into multiple substrings according to the specified delimiter, and returns the array;

Syntax: String.Split (Separator,howmany);

Parameter: separator--required. A string or regular expression that splits a string from the location specified by the parameter;

howmany--is optional. Specifies the maximum length of the returned array, by default returning all elements in the array;

Return value: A string array that returns the string in the array that does not contain separator itself;

Example:

Third, the Operation method

1. Stitching method

(1) concat () function

Function: Used to connect two or more strings and two or more arrays;

Syntax: Arrayobject.concat (Arrayx,arrayx ...);

Parameter: arrayx--required. The parameter can be a specific value, or it can be a data object, can be any number;

Return value: Returns a new string or array

Example:

2. Method of interception

(1) intercept the string according to subscript slice ()

Function: Returns the selected element in an existing string or array according to the subscript;

Syntax: Arrayobject.slice (start,end);

Parameter: start--required. Start subscript, negative when the position is calculated from the end of the array or string;

end--is optional. Ends the subscript, with negative numbers starting at the end of the array or string;

Return value: Returns a new string or array;

Example:

(2) intercept the string according to subscript substring ()

Function: Extracts the characters between the two subscripts

Syntax: String.slice (start,end);

Parameter: start--required. Start subscript, non-negative;

end--is optional. End subscript, non-negative;

Return value: Returns the extracted string;

Example:

(2) Intercept string based on length substr ()

Function: Extracts the specified number of characters from the beginning of the subscript;

Syntax: String.slice (start,length);

Parameter: start--required. Start subscript, negative when the end of the string from the beginning of the position calculated;

length--is optional. The number of characters in a substring. must be numeric;

Return value: Returns the extracted string;

Example:

2. Whitespace handling

(1) Clear string front and suffix whitespace method trim ();

(2) Clear the character left blank Method Trimleft ();

(3) Clear character to the right of the Space Method TrimRight ();

Example:

3. Comparison Method Localecompare ()

Function: Compare two strings in a local specific order

Syntax: Stringvar.localecompare (STRINGEXP);

Parameter: stringvar--required. The first string to compare;

stringexp--required. The second string to compare;

Return value: Returns STRINGVAR and Stringex comparison results (-1: If Stringvar is in front of stringexp; + 1: If Stringvar is in Stringexp, 0 (0) If two characters are equal);

Example:

Third, the coding method

1. String general encoding and decoding escape () and unescapae () functions

(1) Escape () encoding function

Function: Encodes a string so that the string can be read on all computers;

Syntax: Escape (Stringvar);

Parameter: stringvar--required. The string to be escaped or compiled;

Return value: compiled copy of Stringvar;

(2) unescape () encoding function

Function: Decoding the string encoded by the unescape () function;

Syntax: unescape (stringvar);

Parameter: stringvar--required. To reverse-decode or invert a literal string;

Return value: decoded copy of Stringvar;

(1) (2) Example:

2.URI string encoding and decoding encodeURI (), decodeURI () function

(1) encodeURI () encoding function

Function: Encode the string as a URI

Syntax: encodeURI (Stringuri);

Parameter: stringuri--required. A string that contains the URI or other encoded text;

Return value: compiled copy of Stringuri;

(2) decodeURI () decoding function

Function: Decoding the string encoded by the encodeURI () function;

Syntax: decodeURI (Stringuri);

Parameter: stringuri--required. Contains a decoded URI or other text to decode;

Return value: decoded copy of Stringuri;

(1) (2) Example:

2.URI component encoding and decoding encodeURIComponent (), decodeuricomponent () function

(1) encodeURI () encoding function

Function: Encode the string as a URI component

Syntax: encodeURIComponent (Stringuri);

Parameter: stringuri--required. A string that contains the URI or other encoded text;

Return value: compiled copy of Stringuri;

(2) decodeURIComponent () decoding function

Function: Decoding the string encoded by the encodeURIComponent () function;

Syntax: decodeURIComponent (Stringuri);

Parameter: stringuri--required. Contains a decoded URI or other text to decode;

Return value: decoded copy of Stringuri;

(1) (2) Example:

Iv. Method of conversion

1. Convert to uppercase toUpperCase () and tolocaleuppercase () functions

Function: lowercase string to uppercase;

Syntax: Stringvar.touppercase (), stringvar.tolocaleuppercase ();

Return value: A string in which all lowercase characters are converted to uppercase characters;

Example:

2. Convert to lowercase tolowercase () and tolocalelowercase () functions

Function: Convert uppercase string to lowercase;

Syntax: Stringvar.tolowercase (), stringvar.tolocalelowercase ();

Return value: A string in which all uppercase characters are converted to lowercase characters;

Example:

A detailed description of JavaScript string processing

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.