Substrings
It's great that we can get one character using charAt(index), but what if we wanted to get a longer part of a string? A contiguous part of a string is called a substring. The substring(start, end) function gives us a substring starting at the start index and going up to but not including the end index. Use the substring(start, end) function to select the first word in the string stored in the variable message. message stores the string "Hello World".
message.substring(0,5) => 'Hello'
Great! Now we can do something like selecting a word out of a sentence.
No comments:
Post a Comment