+ 1
Finding a character in a certain position of a stringing Javascript
How would you find the character of a certain position of a string in javascript? For instance: var test //would return t
3 Answers
+ 1
var string = "test";
var result = string.charAt(0)
// returns character ar position 0 of string. 0 is 1st character.
If you don't know the position:
var string = "test";
var result = string.indexOf("t");
// returns 0, the position of the first match of t.
// returns -1 if not match is found.
+ 2
Note: like arrays string start indexing at 0
var str = "test";
alert(str[0]); // alerts t
or
alert(str.charAt(0)); // alerts t
+ 1
Certain position
"test"[0];
To find the certain position
"test".split("").indexOf("t");