JS string processing

Source: Internet
Author: User

1. Ary.splice (index,number,x1,x2,......... xn)

Splice function can implement delete/Add/replace of array or string

var a=[1,2,3,4,5,6];var b=a.splice(1,2);console.log(b);//b为返回的删除元素数组,b=[2,3]console.log(a);//splice会之间对元素组a进行操作,这时a=[1,4,5,6]

2. Ary.slice (Start,end)

Slice used to select a segment of an array element

var a=[1,2,3,4,5,6];var b=a.slice(2,5);console.log(b);//输出的b=[3,4,5]

3. Ary.split (Str,number)

Split implements the segmentation of the string, returning the resulting array
The first argument is the character that splits the string, the second argument is the length of the array returned

var str="I,love,you,Do,you,love,me";var str1=str.split(‘,‘,3);console.log(str1);//str1=[‘I‘,‘love‘,‘you‘],输出会显示 I,love,you

4. Querying substrings

Many people will forget the JavaScript's own methods, or forget their specific usage, resulting in the need to nest a for loop to do the problem.
The first function: IndexOf (), which starts from the beginning of the string, finds the corresponding coordinates, and cannot find the return-1, as follows:

var"I,Love,you,Do,you,love,me";var index = myStr.indexOf("you"// 7 ,基于0开始,找不到返回-1

The second function: LastIndexOf (), which starts from the end of the string and finds the corresponding coordinates returned, cannot find the return-1, as follows:

var"I,Love,you,Do,you,love,me";var index = myStr.lastIndexOf("you"// 14

The above two functions also receive the second optional parameter, indicating where to start the lookup.

5. String conversions

String conversion is the most basic requirement and work, you can convert any type of data to a string, you can use any of the following three methods:

var19// 19var// "19"

You can also do this:

var19// 19varString// "19"

Or, just a little bit more simple:

var19// 19var""// "19"

6. String connection

The string join operation can be done simply by using an addition operator, such as:

var"I,love,you!";var"Do,you,love,me?";var"Yes!";//"I,love,you!Do,you,love,me?Yes!"

Similarly, JavaScript has its own functions, such as:

var"I,love,you!";var"Do,you,love,me?";var str = str1.concat(str2);//"I,love,you!Do,you,love,me?"

where the concat () function can have multiple arguments, pass multiple strings, and stitch together multiple strings.

7. String cutting and extraction

There are three ways to extract and cut from a string, such as:
First, use Slice ():

var"I,love,you,Do,you,love,me";var subStr = myStr.slice(1,5);//",lov"

The second type, using substring ():

var"I,love,you,Do,you,love,me";var subStr = myStr.substring(1,5//",lov"

Third, use substr ():

var"I,love,you,Do,you,love,me";var subStr = myStr.substr(1,5//",love"

Unlike the first and second, the substr () second parameter represents the maximum length of the truncated string, as shown in the results above.

8. String comparisons

Compares two strings, compared with rules that are compared in alphabetical order, such as:

var"chicken";var"egg";var// -1first = myStr.localeCompare("chicken"// 0first = myStr.localeCompare("apple"// 1

9. For example

Finally, we look at a front-end pen questions, where to go to the net, I believe many programs have done this problem, title: Write a getsuffix function, used to get the input parameter suffix name, such as input abcd.txt, return txt, attached my answer:

function getSuffix(file){    returnfile.slice(file.lastIndexOf("."1,file.length);}

I believe that the function of string manipulation in JavaScript should be more than this, but the above listed should be very common, if there is any need to add, welcome to add! Hope to see these later, face the string of written test face questions you can very calmly face.

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