A summary of JavaScript string properties and Common methods

Source: Internet
Author: User

Length property: str.length;

Common methods:

1. Str.charat (n)

Finds the nth character in a string, or returns an empty string if it is not between 0~str.length-1

2. Str.indexof (Substr[,start])

Returns the position of the first occurrence of the substr in the string str, starting from the start position, or 1 if it does not exist.

Start can be any integer and the default value is 0. If start < 0 finds the entire string (as if it were passed in 0). If start >= str.length, the method returns-1 unless the string being looked up is an empty string, which returns STR.LENGTH.

3. Str.lastindexof (Substr[,start])

Returns the position of the last occurrence of the substr in the string str, starting from the start position, and returns 1 if it does not exist.

4. Str.substring (Start[,end])

Returns the characters from start to end (not included), and start and end are non-negative integers. If the end parameter (end) is omitted, it is truncated from the start position to the last.

5. Str.slice (start.end) returns the character from start to end (excluding end) and can pass a negative value of 6.  STR.SUBSTR (Start,end) returns the string 7 of the specified length starting at the specified position.  Str.touppercase () Converts the string all to uppercase 8.  Str.tolowercase () converts all strings to lowercase 9. Str.trim () Strips the beginning and end of a string. Str.split ([separator][, limit]) returns an array where the delimiter separator can be a string or regular expression
var str = "Hello?" world! "  //  ["Hello? world! "] // ["H", "E", "L", "L", "O", "?", "W", "O", "R", "L", "D", "!"] // ["Hello", "world!"] // ["H", "E", "L", "L", "O"]

Str.match (RegExp)

Returns an array that contains the matching results, or null if there are no matches. If the parameter passes in an non-regular expression object, it is implicitly converted to a regular expression object using new RegExp (obj)

var str = ' Javascript java '; Str.match (//  ["java"]//  ["Java", "Java"]  //  null

Str.search (RegExp)

Finds if STR matches a regular expression. If the match succeeds, the index of the first occurrence of the regular expression in the string is returned; otherwise, 1 is returned. If the parameter passes in an non-regular expression object, it is implicitly converted to a regular expression object using new RegExp (obj)

var str = ' I love javascript! ' ; Str.search (//  -1//  7//  7//  7

13.str.replace(regexp|substr, newSubStr|function)

To replace a substring of STR

var str = "Do I Love Me", str.replace (//  "Do you Hate Me"

JavaScript string properties and Common methods summary

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.