+ 4

How do i store the first character of a word in a char variable?

18th Apr 2017, 9:56 PM
chris
chris - avatar
3 Réponses
+ 10
char first = word.charAt(0);
18th Apr 2017, 10:04 PM
Rrestoring faith
Rrestoring faith - avatar
+ 7
0 is the starting index, so to get the first character it's always 0. example: String word = "cool burgers"; Index -> char 0 -> 'c' 1 -> 'o' 2 -> 'o' 3 -> 'l' 4 -> ' ' etc.. 12 = word.length() - 1 -> 's' charAt() returns the character at the index in the paranthesis, index 0 being the first char in the string.
18th Apr 2017, 10:42 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
how do I know what number to put in the parenthesis ?
18th Apr 2017, 10:17 PM
chris
chris - avatar