Methods of String Object in JavaScript
JavaScript String
Object
The Most Common Methods
NN: Netscape, IE: Internet Explorer, ECMA:
Web Standard
Methods
|
Explanation
|
NN
|
IE
|
ECMA
|
length
|
Returns the number of characters in a
string
|
2.0
|
3.0
|
1.0
|
indexOf()
|
Returns the index of the first time the
specified character occurs, or -1 if it never occurs, so with that index you
can determine if the string contains the specified character.
|
2.0
|
3.0
|
|
lastIndexOf()
|
Same as indexOf, only it starts from
the right and moves left.
|
2.0
|
4.0
|
|
match()
|
Behaves similar to indexOf and
lastIndexOf, but the match method returns the specified characters, or
"null", instead of a numeric value.
|
4.0
|
4.0
|
|
substr()
|
Returns the characters you specified:
(14,7) returns 7 characters, from the 14th character.
|
4.0
|
4.0
|
|
substring()
|
Returns the characters you specified:
(7,14) returns all characters between the 7th and the 14th.
|
2.0
|
3.0
|
1.0
|
toLowerCase()
|
Converts a string to lower case
|
2.0
|
3.0
|
1.0
|
toUpperCase()
|
Converts a string to upper case
|
2.0
|
3.0
|
1.0
|
Examples
The
length method
The length method returns the number of characters in a string.
The length method returns the number of characters in a string.
<html>
<body> <script type="text/javascript"> var str="Web Enabling Tools is Cool!" document.write("<p>" + str + "</p>") document.write("str.length") </script> </body> </html> |
The
indexOf() method
Test if a string contains a specified character. Returns an integer if it does and -1 if it does not. Use this method in a form validation.
Test if a string contains a specified character. Returns an integer if it does and -1 if it does not. Use this method in a form validation.
<html>
<body> <script type="text/javascript"> var str="Web Enabling Tools is Cool!" var pos=str.IndexOf("Enabling") if (pos>=0) { document.write("School found at position: ") document.write(pos + "<br>") } else { document.write("Enabling not found!") } <p>This example tests if a string contains a specified word. If the word is found it returns the position of the first character of the word in the original string. Note: The first position in the string is 0! </script> </body> </html> |
The
match() method
Works similar to the indexOf method, only this method returns the characters you specified, "null" if the string does not contain the specified characters.
Works similar to the indexOf method, only this method returns the characters you specified, "null" if the string does not contain the specified characters.
<html>
<body> <script type="text/javascript"> var str="Web Enabling Tools is cool!" document.write(str.match("cool")) </script> <p>This example tests if a string contains a specified word. If the word is found it returns the word! </body> </html> |
The
substr() method
Returns a specified part of a string. If you specify (3,6) the returned result string will be from the third character and 6 long. (Note that since the first character is 0, the second is 1 etc, the result will be from the second character and 6 long).
Returns a specified part of a string. If you specify (3,6) the returned result string will be from the third character and 6 long. (Note that since the first character is 0, the second is 1 etc, the result will be from the second character and 6 long).
<html>
<body> <script type="text/javascript"> var str="W3Schools is great!" document.write(str.substr(2,6)) document.write("<br><br>") document.write(str.substring(2,6)) </script> <p>The substr() method returns a specified part of a string. If you specify (2,6) the returned string will be from the second character (start at 0) and 6 long. <p>The substring() method also returns a specified part of a string. If you specify (2,6) it returns all characters from the second character (start at 0) and up to, but not including, the sixth character. </body> </html> |
The
substring() method
Returns a specified part of a string. (3,6) returns the characters from the third to the 6th.
Returns a specified part of a string. (3,6) returns the characters from the third to the 6th.
<html>
<body> <script type="text/javascript"> var str="W3Schools is great!" document.write(str.substring(3,6)) </script> <p>This example returns all the characters from the third character up to but not including the 6th character. (Note that since the first character is 0, the second is 1 etc, the result will be from the fourth character including the 6th character). </body> </html> |
The
toLowerCase() and toUpperCase() methods
Converts a string to lower case and upper case respectively.
Converts a string to lower case and upper case respectively.
<html>
<body> <script type="text/javascript"> var str=("Hello JavaScripters!") document.write(str.toLowerCase()) document.write("<br>") document.write(str.toUpperCase()) </script> </body> </html> |
No comments:
Post a Comment