Js string processing

Source: Internet
Author: User

Js string processing

1. ary. splice (index, number, x1, x2 ,......... Xn)

The splice function can delete, add, or replace arrays or strings.

Var a = [1, 2, 3, 4, 5, 6]; var B =. splice (1, 2); console. log (B); // B is the returned array of deleted elements, B = [2, 3] console. log (a); // splice performs operations on element group a. In this case, a = [,]

2. ary. slice (start, end)

Slice is used to select a segment of the array element

Var a = [,]; var B = a. slice (); console. log (B); // output B = [, 5]

3. ary. split (str, number)

Split splits strings and returns the array after splitting.
The first parameter is the delimiter of the string, and the second parameter is the length of the returned array.

Var str = "I, love, you, Do, you, love, me"; var str1 = str. split (',', 3); console. log (str1); // str1 = ['I', 'love', 'you']. The output displays I, love, you

4. query substrings

Many people will forget the built-in JavaScript methods or their specific usage, so that they have to nested a for loop during Question preparation.
The first function: indexOf (), which starts from the start of the string and returns the corresponding coordinates. If-1 is not found, the returned result is as follows:

Var myStr = "I, Love, you, Do, you, love, me"; var index = myStr. indexOf ("you"); // 7, starting from 0, cannot find-1

The second function: lastIndexOf (), which starts from the end of the string and returns the corresponding coordinates. If no coordinate is found,-1 is returned, as shown below:

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

The above two functions also receive the second optional parameter, indicating the location where the search starts.

5. String Conversion

String Conversion is the most basic requirement and work. You can convert any type of data to a string. You can use either of the following three methods:

var num= 19; // 19var myStr = num.toString(); // "19"

You can do the same:

var num= 19; // 19var myStr = String(num); // "19"

Or, just a little simpler:

var num= 19; // 19var myStr = "" +num; // "19"

6. String connection

The string connection operation can be done simply by using an addition operator, for example:

var str1 = "I,love,you!";var str2 = "Do,you,love,me?";var str = str1 + str2 + "Yes!";//"I,love,you!Do,you,love,me?Yes!"

Similarly, JavaScript also comes with related functions, such:

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

The concat () function can have multiple parameters, pass multiple strings, and splice multiple strings.

7. String cutting and Extraction

There are three methods to extract and cut strings, such:
First, use slice ():

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

Second, use substring ():

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

Third, use substr ():

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

Different from the first and second types, the second parameter of substr () represents the maximum length of the intercepted string, as shown in the preceding results.

8. String comparison

Compare two strings in alphabetical order, for example:

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

9. Example

Finally, let's take a look at a front-end pen question. I believe many programs have done this. Question: Write A getsuffix.pdf, use it to input a suffix, for example, input abcd.txt, return txt, and attach my answer:

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

I believe that the string-operated functions in JavaScript should be more than these functions, but these functions listed above should be very common. If you need to add them, please add them! We hope that you will be able to face the string's written examination questions after seeing these questions.

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.