Selecting Characters
Imagine that you have a list of strings containing words and you want to sort them alphabetically. You'll need a way to get the first character in each string. You might even need the second or third character if there are multiple words with the same first letter. The charAt(index) is a handy way to get the character at a specific position in a string. Keep in mind that strings are zero-indexed, meaning the first position is position 0, the next one is position 1, and the last one is position length - 1. For example, we can get the character at position 2 in the string "hello" by doing "hello".charAt(2). What is the last character in the string stored in the variable ls?
ls.charAt(ls.length-1) => 't'
Good Job!
No comments:
Post a Comment