charAt Function:
Using the charAt method you can find out the element in a string.
Syntax:
variablename.charAt(indexnumber);
Example in HEAD section:
<html> <head> <title>charAt Function</title> <script language="javascript"> var anystring="Hello World"; document.write("The character at index 0 is " +anystring.charAt(0)+"<br>"); document.write("The character at index 1 is " +anystring.charAt(1)+"<br>"); document.write("The character at index 2 is " +anystring.charAt(2)+"<br>"); document.write("The character at index 3 is " +anystring.charAt(3)+"<br>"); document.write("The character at index 4 is " +anystring.charAt(4)+"<br>"); document.write("The character at index 5 is " +anystring.charAt(5)+"<br>"); document.write("The character at index 6 is " +anystring.charAt(6)+"<br>"); document.write("The character at index 7 is " +anystring.charAt(7)+"<br>"); document.write("The character at index 8 is " +anystring.charAt(8)+"<br>"); document.write("The character at index 9 is " +anystring.charAt(9)+"<br>"); document.write("The character at index 10 is " +anystring.charAt(10)); </script> </head> </html> |
Example in BODY Section:
<html> <head> <title>charAt Function</title> </head> <body> <script language="javascript"> var anystring="Hello World"; document.write("The character at index 0 is " +anystring.charAt(0)+"<br>"); document.write("The character at index 1 is " +anystring.charAt(1)+"<br>"); document.write("The character at index 2 is " +anystring.charAt(2)+"<br>"); document.write("The character at index 3 is " +anystring.charAt(3)+"<br>"); document.write("The character at index 4 is " +anystring.charAt(4)+"<br>"); document.write("The character at index 5 is " +anystring.charAt(5)+"<br>"); document.write("The character at index 6 is " +anystring.charAt(6)+"<br>"); document.write("The character at index 7 is " +anystring.charAt(7)+"<br>"); document.write("The character at index 8 is " +anystring.charAt(8)+"<br>"); document.write("The character at index 9 is " +anystring.charAt(9)+"<br>"); document.write("The character at index 10 is " +anystring.charAt(10)); </script> </body> </html> |
OUTPUT
These lines display the following:
The character at index 0 is H
The character at index 1 is e
The character at index 2 is l
The character at index 3 is l
The character at index 4 is o
The character at index 5 is
The character at index 6 is W
The character at index 7 is o
The character at index 8 is r
The character at index 9 is l
The character at index 10 is d
No comments:
Post a Comment