JS String related methods (first write, if there is a similarity, that is the article borrowed from)

Source: Internet
Author: User
Tags first string

1. Character Method

CharAt () and charCodeAt (), both receive a parameter and are based on the 0 character position, charAt () returns the character at the given position, and charCodeAt () returns the character encoding of the given position character. One method that corresponds to charCodeAt () is Formcharcode () converts the character encoding to a string.
Such as:

<script>    var code = "future"; console.log(code.charAt(2)); //t console.log(code.charCodeAt(2)) //116</script>

In addition to the above methods, you can also access the characters in a particular position through square brackets, such as:

<script>    var code = "future"; console.log(code[2]); //t</script>

The method of using square brackets is supported in both Ie8,firefox,safari,chrome and opera, but is not supported in IE7 and earlier, and the returned value is undefined.

2. String manipulation methods

1, String splicing: Concact () method.

"my ";console.log(a.concat("future"));//my futureconsole.log(a.concat("future ", "will"));//my future will

2. Create a new string based on the substring: Slice (), substr (), and substring (). All three methods return a substring of the manipulated string and receive one to two arguments.
The first parameter specifies the starting position of the string, and the second argument (which can be omitted) indicates where the substring ends. The second parameter of Slice () and substring () specifies the position after the last character of the substring, and the second parameter of substr () specifies the number of characters returned. If the second argument is omitted, the last position of the string is truncated.

Varstr ="We are Family"; console.LogStr.slice (2,5));AR (starting from 2nd position, not including 5th position) Console.Logstr.slice (1)); //e is family (starting from the 1th position, until the end) console. log (str.substring (3)); //are family (starting from 3rd position, until the end) console. log (str.substring (3,5)); //ar (starting from 3rd position, not including 5th position) Console. log (str.substr (2)); //is family (starting from the 2nd position, until the end) console. log (str.substr (2,6)"); //is f (starting from 2nd position, intercept 6 characters)          

The slice () and substring () methods have the same functionality in positive cases, but behave differently when the values passed are negative.
The slice () method adds an incoming negative value to the length of the string (so if two arguments are negative, the second argument must be greater than the first argument, otherwise the empty string is returned)
The substr () method adds a negative first argument to the length of the string and converts the second argument to 0. (Returns an empty string if the second argument is a negative value)
The substring () method converts all negative values to 0,substring and always takes the smaller arguments as the starting position.

Varstr ="Wearefamily";Length is 11console.LogStr.slice (-3,-8));Equivalent to Str.slice (8,3)//return empty string console.LogStr.slice (-5,-1));//equivalent to Str.slice (6,10)//Return to Amiconsole. log (str.substring (3,-4));  Equivalent to Str.substring (3,0)//return weaconsole. log (str.substring (-3,-5));  Equivalent to Str.substring (0,0)//return empty string console. log (str.substr (-4,3));  Equivalent to substr (7,3)//return milconsole. log (str.substr (-4,-3));  Equivalent to substr (7,0)//Return empty string               
3. The position method of the string

The location method of the string: IndexOf () and LastIndexOf (). Both methods search for the given substring from a string, and then return the position of the substring, not found, return -1.indexof () search backwards from the beginning of the string, and LastIndexOf () search the substring forward from the end of the string.

str = "WEAREFAMILY";console.log(str.indexOf("A"));//2console.log(str.lastIndexOf("A"));//6
4.trim () method

Trim () to remove whitespace from string wrapping

5. String Case Conversion method

Convert to uppercase: toUpperCase () and Tolocaluppercase ()
Convert to lowercase: tolowercase () and Tolocallowercase ()

6. Pattern Matching of strings

Match (), replace (), search (), replace (), and the split () method are several methods that are defined by the string type in the set used to match patterns in a string.   The
match method receives only one parameter, either a regular expression or a RegExp object. Calling the match () method on a string is essentially the same as calling RegExp's exec () method, which returns an array of

var STR = Span class= "hljs-string" > "we are Weamy"; var pattern =/we/;var matches = str.match (pattern); console. log (matches); //["we", index:0, Input: "We are Weamy"]console. log (pattern.exec (str));  ["We", index:0, Input: "We are Weamy"]console. log (Matches.index); //0console. log (Matches[0]);  We                

The parameter of search () is the same as match (), except that search () returns the index of the first occurrence in the string, or 1 if no match is found.
Replace () method, receive 2 parameters, the first parameter can be a RegExp object or a string, the second argument is a string or a function, if the first argument is a string, then only replace the first string, if you want to replace all the strings, Then you must use a regular expression and specify the global flag (g)

str = "cat, fat, sat, bat";console.log(str.replace("at","ooo"));//cooo, fat, sat, batconsole.log(str.replace(/at/g,"ooo"));//cooo, fooo, sooo, booo

If the second parameter of replace () is a string, you can also use some special sequence of characters to insert a value from the regular expression operation into the result character.
$$ $
s& matching substrings of the entire pattern
Sn matches sub-string of nth capturing group

str = "cat, fat, sat, bat";console.log(str.replace("at","ooo"));//cooo, fat, sat, batconsole.log(str.replace(/(.at)/g,"ooo ($1)"));//ooo (cat), ooo (fat), ooo (sat), ooo (bat)console.log(str.replace(/(.at)/g,"ooo ($&)"));//ooo (cat), ooo (fat), ooo (sat), ooo (bat)

The split () method takes a parameter as a delimiter and transforms the string into an array.
The corresponding method is join (), which is used to convert the array to a string.

7. Localcompare () method

This method is used for string comparisons.
Returns a negative number if the string is to be photographed in the alphabet before the string parameter. (In most cases 1, the specific value depends on the implementation)
Returns 0 if the string equals the string argument.
Returns a positive number if the string is in the alphabetical order after the string argument. (In most cases 1, the specific value depends on the implementation)

str = "break";console.log(str.localeCompare("future")); //-1console.log(str.localeCompare("above")); //1console.log(str.localeCompare("break")); //0

JS String related methods (first write, if there is a similarity, that is the article borrowed from)

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.