0

How to target a single character in a string?

Example target: any third character. string: "Hello World". result: "l".

28th Jul 2019, 4:14 AM
Alex Schneider
Alex Schneider - avatar
5 Antworten
+ 5
You don't need to loop in JavaScript (or in most languages actually). There are a couple ways you can accomplish this. Using the [] operator or using the charAt() method. let a = "hello"; a[2] a.charAt(2)
28th Jul 2019, 4:51 AM
Zeke Williams
Zeke Williams - avatar
+ 2
I never seen the at method... I remember only charAt
28th Jul 2019, 4:53 AM
KrOW
KrOW - avatar
+ 1
I meant to type charAt KrOW . Thanks!
28th Jul 2019, 4:54 AM
Zeke Williams
Zeke Williams - avatar
+ 1
Thanks Zeke Williams It really worked.
28th Jul 2019, 4:59 AM
Alex Schneider
Alex Schneider - avatar
0
You will have to loop through the string to make sure your target is always on the third character. Example:- var i; var name = "Alex"; for(i = 0; i < name.length, i++) { if(name[2]) { document.write("The third character is " + name[2]); } } I didn't test it but it should give u an idea of what you want
28th Jul 2019, 4:38 AM
Franky BrainBox
Franky BrainBox - avatar