eg:
Copy code code as follows:
<html>
<body>
<script type = "text/javascript">
Var Str = "Hello World!"
document.write ("The First Character is:" + Str.charat (0) + "<br />")
document.write ("The Second Character is:" + Str.charat (1) + "<br />"))
document.write ("The Third Character is:" + Str.charat (2))
</script>
</body>
</html>
result:
The First Character is: H
The Second Character is: E
The third character is: l
Definition and usage
The charAt() method returns characters at the specified position.
Please note that JavaScript does not have a character data type different from the string type, so the character returned is a string with a length of 1.
grammar
StringObject.charat (index)
Parameter description
Index must. It represents the number in a position in a string, that is, the bidding of the character in the string.
Tips and notes
Note: The first character in the string is 0. If the parameter INDEX is not between 0 and String.length, this method will return an empty string.
Instance
In the string "Hello World!", We will return to the character 1 of position 1:
Copy code code as follows:
<script type = "text/javascript">
Var Str = "Hello World!"
document.write (str.charat (1))
</script>
The output of the above code is:
E
Return the characters at the specified index position.
Strobj.charat (index)
parameter
strObj
Required option. Any String object or text.
index
Must be options. The zero -based indexes you want. The valid value is the value between 0 and the length of the string.
illustrate
The Charat method returns a character value, which is located at the specified index position. The index of the first character in the string is 0, the second index is 1, and so on. The index value beyond the effective range returns an empty string.
Exemplary
The following example illustrates the usage of the charAt method:
Copy code code as follows:
function charattest (n) {
VAR Str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // Initialize variables.
var s; // reputation variables.
s = str.charat (n -1); // From the position of the index to n 1
// Get the correct character.
return(s); //Return character.
}