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 property
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 a single character in the *th position of the string
No further description
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 character from the string String object, the position is returned if the search is successful, otherwise -1 is returned
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, searching from back to front
document.write("children".lastIndexOf("l ",4)); //The output result is 3
split (separator character): Returns an array. The array is separated from the string. The separator character determines where to separate. .
'l&o&v&e'.split('&'); //Returns the array l,o,v,e
substring(): Equivalent to the cutting function of string
substring(
[,])
document.write("children".substring(1,3)); //The output result is hil
substr(): also equivalent to cropping, please note the difference with substring()
substr([,])
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 and in the latter are uppercase.
document.write("LOVE".toLowerCase()); //The output result is love
document.write("love".toUpperCase()); //The output result is LOVE
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn