JavaScript string interception method

Source: Internet
Author: User

1 Character method

CharAt (): receives a parameter that returns the character at the given position

charCodeAt (): receives a parameter that returns the character encoding of the specified position character

var str = "Hello World"; alert (Str.charat (1));    Ealert (Str.chatcodeat (1))    //101

  

2 String manipulation methods

  concat (): stitching one or more strings together. String.Concat (str)

  Slice (start,[stop]): receives one or two parameters, starting position and end position, returning a substring of the manipulated string

  substr (start,n): receives two parameters, starting position and number of return characters , returns a substring of the manipulated string

  substring (start,[stop]): receives one or two parameters, starting position and end position, returning a substring of the manipulated string

The next three methods behave differently when passed in a negative value, slice () adds the passed negative value to the string length, substr () adds the first negative argument to the string bed, and the second negative argument to 0,substring () converts the negative argument to 0.

var str = "Hello World" alert (Str.slice ( -3)),//rldalert (str.substring ( -3)),//hello Worldalert (Str.substr (-3));// Rldalert (Str.slice (3,-4));//lowalert (Str.substring (3,-4));//helalert (Str.substr (3,-4));/"" (Empty string)

3 String Position method

IndexOf () lastIndexOf () accepts two parameters, the character to search for and the starting position

String.IndexOf (' a ')//a indicates the character to be searched

4 Trim () method

The method creates a copy of the string, removes all whitespace from the predecessor and suffix, and returns the result

5 String Case Conversion method

toLowerCase ()

toLocaleLowerCase ()

toUpperCase ()

toLocaleUpperCase ()

6 Matching methods for strings

Match (): finds a match for one or more regular expressions, returns an array of String.match (regular expressions)

Search (): Replaces a substring that matches a regular expression, returns the first matching index value in a string, if no 1 string.search (regular expression) is returned

Replace (): ' Retrieves a value that matches a regular expression, string.replace (regular expression, replaced character)

JavaScript string interception method

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.