The Sting string object is one of the built-in objects provided by Javascript.
Pay special attention here, the first character in the string is the 0th character, and the second character is the 1st character.
1. Method to create a string object
[var] String object instance name = new String(string)
Or var String object instance name = string value
example:
var str = "Hello World";
var str1 = new String("This is a string");
2.String properties
length: returns the length of the string
var intlength = str.length //intlength = 11
3.String method
charAt(*): Returns the single character at the *th position of the string
var x = "abcdefg"; var y = x.charAt(3); //y="d"
charCodeAt(*): Returns the ASCII code of the single character at the *th position of the string
No further details
Copy the code code as follows:
fromCharCode(): Accepts a specified Unicode value and returns a string.
document.write(String.fromCharCode(72,69,76,76,79)); //The output result is HELLO
indexOf(): Find another string object from a string, return the position if the search is successful, otherwise return -1
document.write("children".indexOf("l",0)); //The output result is 3
document.write("children".indexOf("l",1)); //The output result is 3
document.write("children".indexOf("l",4)); //The output result is -1
lastIndexOf(): similar to the indexOf() method, except that the search direction is opposite, from back to front
document.write("children".lastIndexOf("l",4)); //The output result is 3
split(separator character): Returns an array that is separated from the string. The separator character determines where to separate.
'l&o&v&e'.split('&'); //return array l,o,v,e
substring(): Equivalent to the cutting function of string
substring(<start>[,<end>])
document.write("children".substring(1,3)); //The output result is hil
substr(): also equivalent to cropping, please note the difference with substring()
substr(<start>[,<length>])
Copy the code code as follows:
document.write("children".substr(1,3)); //The output result is hil. It should be noted here that compared with substing, although the results are the same, the algorithms and ideas are different.
toLowerCase() and toUpperCase(): have similar functions, except that they return a string with the same original string. The only difference is that all letters in the former are lowercase, while in the latter they are uppercase.
document.write("LOVE".toLowerCase()); //The output result is love
document.write("love".toUpperCase()); //The output result is LOVE