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:
- 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"
- Str1.touppercase (); --Get str1 all converted to uppercase results
var s2 = str1.touppercase (); The result of S2 is: "ABCDEFGABC"
- Str1.tolowercase (); --Get str1 all converted to lowercase results
var s3 = Str1.tolowercase (); The result of S3 is: "ABCDEFGABC"
- 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"
- 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.
- 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.
- 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"
- 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