+ 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

26th Mar 2018, 7:37 PM
Leroy Jenkins
Leroy Jenkins - avatar
3 Respostas
+ 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.
26th Mar 2018, 7:44 PM
Adam
Adam - avatar
+ 2
Note: like arrays string start indexing at 0 var str = "test"; alert(str[0]); // alerts t or alert(str.charAt(0)); // alerts t
26th Mar 2018, 7:44 PM
TurtleShell
TurtleShell - avatar
+ 1
Certain position "test"[0]; To find the certain position "test".split("").indexOf("t");
26th Mar 2018, 7:44 PM
Toni Isotalo
Toni Isotalo - avatar