Sample usage of js charAt
- 2020-03-30 01:44:14
- OfStack
Eg:
Results:
The first character is H
The second character is: e
The third character is: l
Definition and usage
The charAt() method returns a character at the specified location.
Note that JavaScript does not have a character data type that is different from a string type, so the character returned is a string of length 1.
grammar
StringObject. CharAt (index)
Parameters to describe
The index required. A number representing a position in a string, that is, the subscript of a character in a string.
Hints and comments
Comment: the subscript of the first character in the string is 0. If the parameter index is not between 0 and string.length, the method returns an empty string.
The instance
In the string "Hello world!" , we will return the character of position 1:
The output of the above code is:
e
Returns the character at the specified index position.
StrObj. CharAt (index)
parameter
strObj
Will be options. Any String object or literal.
The index
Will be options. Zero-based index of the desired character. The valid value is between 0 and the length of the string minus 1.
instructions
The charAt method returns a character value at the specified index position. The first character in a string has an index of 0, the second has an index of 1, and so on. An index value that is out of range returns an empty string.
The sample
The following example illustrates the use of the charAt method:
<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>
Results:
The first character is H
The second character is: e
The third character is: l
Definition and usage
The charAt() method returns a character at the specified location.
Note that JavaScript does not have a character data type that is different from a string type, so the character returned is a string of length 1.
grammar
StringObject. CharAt (index)
Parameters to describe
The index required. A number representing a position in a string, that is, the subscript of a character in a string.
Hints and comments
Comment: the subscript of the first character in the string is 0. If the parameter index is not between 0 and string.length, the method returns an empty string.
The instance
In the string "Hello world!" , we will return the character of position 1:
<script type="text/javascript">
var str="Hello world!"
document.write(str.charAt(1))
</script>
The output of the above code is:
e
Returns the character at the specified index position.
StrObj. CharAt (index)
parameter
strObj
Will be options. Any String object or literal.
The index
Will be options. Zero-based index of the desired character. The valid value is between 0 and the length of the string minus 1.
instructions
The charAt method returns a character value at the specified index position. The first character in a string has an index of 0, the second has an index of 1, and so on. An index value that is out of range returns an empty string.
The sample
The following example illustrates the use of the charAt method:
function charAtTest(n){
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Initialize the variable.
var s; //Name the variable.
s = str.charAt(n - 1); //From where the index is n, w1
//Gets the correct character.
return(s); //Returns a character.
}