Properties and methods of String object
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var str="ASDfgh"; document.write(str.length+"<br/>"); document.write(str.toLowerCase()+"<br/>"); document.write(str.toUpperCase()+"<br/>"); </script> </head> <body> </body> </html>
##charAt(index)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var str="Hello JavaScript"; for(var i=0;i<str.length;i++){ document.write(str.charAt(i)+"<br/>"); } </script> </head> <body> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var str="Hello JavaScript"; document.write(str.indexOf("J")+"<br/>"); document.write(str.indexOf("S")+"<br/>"); document.write(str.indexOf("s")+"<br/>"); </script> </head> <body> </body> </html>
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var str="Hello JavaScript"; document.write(str.lastIndexOf("J")+"<br/>"); document.write(str.lastIndexOf("S")+"<br/>"); document.write(str.lastIndexOf("s")+"<br/>"); </script> </head> <body> </body> </html>
##substr()
Function: In the original string, return a substring.
Syntax: strObj.substr(startIndex [ , length])
Description: Return length characters starting from the subscript startIndex.
Parameters:
length is optional, how many characters are returned. If omitted, always returns to the end.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var str="Hello JavaScript"; document.write(str.substr(6)+"<br/>"); document.write(str.substr(6,4)+"<br/>"); </script> </head> <body> </body> </html>
##substring()
Function: In the original string, return a substring.
startIndex: represents the start index value.
endIndex: optional, representing the end index value. If omitted, all characters up to the end are generally returned.
Note: If the second parameter is omitted, the results of substr() and substring() are the same.
Note: The characters returned by substring() include the characters at startIndex and do not include the characters at endIndex.
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var str="Hello JavaScript"; document.write(str.substring(6)+"<br/>"); document.write(str.substring(6,10)+"<br/>"); </script> </head> <body> </body> </html>
split(): Convert a string into an array
Function: Cut a string into several segments. Returns an array.
Syntax: strObj.split (split number)
Parameters: The parameter is a string of split numbers. Cut the string into segments using the specified split number.
Use a space as the dividing number to divide the string Hello JavaScript
into an array Hello, JavaScript
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var str="Hello JavaScript"; document.write(str.split(" ")) </script> </head> <body> </body> </html>