On the manipulation of strings in JavaScript

Source: Internet
Author: User

I think, the most developers, the most practical development process, any language in the development of the actual project process, is not open string operation of the following author of his daily development process used in some of the string operation method to do some statements and summary, of course, if the reader feels that there are shortcomings, Very welcome to Enlighten!!!

The first is some copy of the string method, note Oh, classmate, the title of the main list are some shallow copies, that will not affect the original array:

1:slice () 2:substr () 3:substring ()

Since the above three methods have their common, the main topic is not introduced alone, together:

varstr ="abcdefg,hijklmn,opqrst,uvwxyz"; varNewstr = Str.slice (1,3);//does not contain characters that are referred to in the following argument, shallow copyConsole.log (NEWSTR);//BCConsole.log (str);//abcdefg,hijklmn,opqrst,uvwxyz        varNEWSTR2 = Str.substr (1,3);//contains the characters that are referred to in the following element, shallow copyConsole.log (NEWSTR2);//BCDConsole.log (str);//abcdefg,hijklmn,opqrst,uvwxyz        varNEWSTR3 = str.substring (1,3);//does not contain characters that are referred to in the following argument, shallow copyConsole.log (NEWSTR3);//BCConsole.log (str);//abcdefg,hijklmn,opqrst,uvwxyz

Code for proof, from the above running results can be seen, slice and substring two functions will not contain the elements referred to in the following parameters, and substr will, secondly, these methods in the parameter is negative when there are some differences, here no longer discussed, the reader self-check.

Secondly, let's talk about the string manipulation in JS I think a more powerful function, that is the Split function, the following is described:

Split () Function: split() The method String splits an object into a string array by splitting the string into substrings.

Grammar:str.split([separator][, limit])  

Parameter separator: Specifies the character (string) used to split the string. separatorcan be a string or a regular expression. If omitted separator , the returned array contains an element consisting of the original string. If separator it is an empty string, it str will be converted into an array of characters from the original string.

Parameter limit: An integer that defines the number of split fragments returned. The split method still splits each match separator , but the returned array will only intercept the most limit elements.

Example

Character Locator function:

IndexOf () and LastIndexOf () start looking for different directions, one from the front to the back, and one from behind to go forward.

Case conversion:

toUpperCase (), toLowerCase ()

Get character Method:

CharAt (): Returns the character of a custom position

charCodeAt (): Get character encoding (parameter is subscript)

Stringvalue[]: Direct subscript access

Of course, there are pattern matching methods:

Match (): Fan Hu An array the first item is a string that matches the entire pattern, after which each item holds a string that matches the capturing group in the regular expression

Search () method, which returns the match index of the first item involving a regular expression

Replace () method: Another very powerful method:

On the manipulation of strings in JavaScript

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.