Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Grigorij Schleifer
10,365 PointsDifference between charAt() and indexOf() in Java ?
I don`t understand the difference between this two sexy methods. Do i use indexOf() for analysis a char in a String ?
And charAt() only for chars, in char-arrays ?
2 Answers
Dan Johnson
40,533 PointsThe charAt method will retrieve the character at the index you give it. For example if you were to run this in a REPL:
"MyString".charAt(2);
You'll get back 'S', the character at index 2 of the String. indexOf is basically the inverse:
"MyString".indexOf('S');
Will give back 2, the location of the character 'S' in the String.
Grigorij Schleifer
10,365 PointsThis is great ... thank you, Dan !!!!