JS string Common Properties and methods

Source: Internet
Author: User

String declaration:

var str1 = new String ("Abcdefgabc"); This is a "string object"

var str2 = "Abcdefgabc"; This string is almost indistinguishable from the front str1.

str.length--gets the length of a string (that is, the number of characters)

Methods for string objects:

  1. Str1.charat (n);                --Get the character with the position n in the string str1 (the position of the character is also starting from 0) var S1 = Str1.charat (3); The result of the S1 is: "D"
  2. Str1.touppercase (); --Get str1 all converted to uppercase results
    var s2 = str1.touppercase (); The result of S2 is: "ABCDEFGABC"
  3. Str1.tolowercase (); --Get str1 all converted to lowercase results
    var s3 = Str1.tolowercase (); The result of S3 is: "ABCDEFGABC"
  4. Str1.replace ("character 1", "character 2"); --Replace "character 1" in str1 with "character 2"
    var S4 = Str1.replace ("CD", "999"); The result of S4 is: "AB999EFGABC"
  5. Str1.indexof ("character 1");---Get "character 1" first occurrence in str1, if not present, the result is-1
    var S5 = str1.indexof ("ab"); The result of S5 is 0.
  6. Str1.lastindexof ("" Character 1 ");---get the last position of" character 1 "in str1, if not present, the result is-1
    var s6 = str1.lastindexof ("ab"); The result of S6 is 7.
  7. Str1.substr (n, m)--Gets the m character starting from position N in str1, M can be omitted, then it is taken from position N to the end of the string--note that this "fetch" does not affect the original character of STR1
    var s7 = Str1.substr (2, 4); S7 as: "Cdef"
  8. Str1.substring (n, m)--Gets the first character from position N to position m in str1.
    var s8 = str1.substring (2, 4); S8 as: "CD"

Str1.split ("character 1")--divides str1 into an array with the specified "character 1", resulting in an array
var S9 = Str1.split ("B"); The result of S9 is an array: ["A", "CDEFGA", "C"]

Property length

CharAt () Gets the character where the string position is n Str.charat (n);

toUpperCase () Gets the result of the character being capitalized

Tolowercae () Gets the result of a string converted to lowercase

Replace () the character of argument 1 in the string is replaced with the character of parameter 2 and returns Str.replace (A, b)

IndexOf ()

LastIndexOf () returns a string where the first and last occurrences of the position did not match to the return -1,str.indexof ('. ')

SUBSTR () obtains characters from N to M directly, the M parameter can be omitted (last)

SUBSTRING () gets the N to m-1 character

Trim ()

Split ()

     Cut Array

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.