From the course: Nail Your Java Interview

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Access data from strings

Access data from strings - Java Tutorial

From the course: Nail Your Java Interview

Access data from strings

- [Instructor] Now that we know some ways to normalize our input. Let's talk about how we can access the data inside of our string. Much of this functionality is already built in with string instance methods. Most of these methods use indexing for accessing each character stored in the string. Looking at the first variable here, we could say that A is a index zero, P is a index one, P is a index two and so on for the rest of the string. The last index in the string is the length of the string minus one. So here for apples, since the length is six the last index must be five because we start our counting at zero. To access a character at a given index we can use the strings charAt method. Let's say we want to access the first character. All right, char first character, create a variable. We'll access the string, apples, use charAt, that built in functionality and our input will be zero. Because the first…

Contents