From the course: Learning Java 11

Unlock the full course today

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

Solution: Find last character

Solution: Find last character - Java Tutorial

From the course: Learning Java 11

Solution: Find last character

- [Instructor] Let's find the last character in the string. When approaching a problem like this, I find it helpful to write down some of the facts we know. First, we know that characters are accessed by index. In fact, we can access a given character with the charAt method. (keyboard faintly clicking) The last character in the string is located at the length of the string minus one. (keyboard faintly clicking) We can combine these two ideas to find the last character in the string. The string in question is the text, so we'll run charAt on that string. (keyboard faintly clicking) The input for charAt is an index, and it will return the character at that index. We know the last character lives at the length of the string minus one, so let's add that. To get the length of the string, we'll use text.length. Then we subtract one here because indices start at zero. This will give us the last character in the string. Let's run it. Our code works as expected. Now, you might be thinking that…

Contents