 In many programs, you'll need to extract portions of a string or find out whether strings are contained in other strings. Let's look at portions of a string called substrings first. We extract these substrings using the substring method. When you give the substring method one number, the result is the section from that position to the end of the string. If you give two numbers, you get the section starting at the start index up to but not including the end position. Here's a specific example. The word flashpoint with its character indices numbered. Word.substring5 gets the segment of the string starting at index number five all the way to the end. Word.substring2,5 starts at position two up to but not including position five, which gives you the word ash. To find out if a character or string is contained within another string, you use the index of method. If the character or string is found, it returns the index number where it starts. If not found, it returns negative one. Here's an example. In the string pre-reading, the dash is at index three and the string re starts at index position one. The character x is not in the string and neither is the word real, so these both return negative one. Ordinarily, index of starts looking at the beginning of the string. You can tell index of where to start looking by giving it a starting index position. In this example, the first n, at or after position three, is the n at index number four. Starting at index two, the string an is found starting at position three. If we try finding the letter n starting at index five, there is none and there's no an after index four in the word. You can tell Java to start looking backwards from the end of the word by using the last index of method. In this example, we try to find the first dash looking backwards from the end of the string. It's at position seven. Similarly, the first at looking backwards from the end of the string starts at index nine. If we look backwards for a dash starting at index five, it's found at index three. And if we look backwards for at starting at index eight, we'll find it at index five. And here's an example where it's not found if we try to find a dash looking backwards from position two. Okay, let's put this all together. We'll ask the person at the keyboard to enter up to three words separated by commas and we'll split them up. There are easier methods to do this, but I want a nice example program. Here's the plan. Look for the first comma using index of and remember its position. Take the substring starting at the beginning and save it. Now starting one position past the place where we found the comma, search for another comma. Take that substring as our second word after terming blanks. Starting one space after the second comma, we take the substring to the end of the input and that's our third word. Let's look at a program that implements this. Instead of trying to do the whole program at once, we'll build it up in parts. In lines 11 and 12, we prompt for and read a phrase from the user. In lines 14 through 16, we initialize our three words to the empty string. On line 18, we find the first comma in the phrase by using index of. If there is a comma, that index will be greater than or equal to zero, and we use the substring method starting at the beginning of the phrase up to the first comma, but not including it, and that's assigned to word one. If the first comma is negative, that means there wasn't a comma anywhere and the whole phrase becomes our first word. There are no second and third words. We'll then trim leading and trailing spaces from all our words and display them. Let's compile that and run it and try it with three words. B, horse, toucan, and it found the first word correctly. Now that we know that this works, we can add the remainder of the program. We need to find the position of the second comma. That becomes the index of a comma in the phrase, starting where we found the first comma plus one place past the first comma. If we found one, second comma will be greater than or equal to zero. We'll extract the second word. It'll become the phrase substring, starting one place past the first comma, up to but not including the place where we found our second comma. The third word will become the substring of the phrase, starting one past the second comma to the end of the word. If we didn't have a second comma, the second word becomes the substring of the phrase, starting one past the first comma to the end. Let's recompile and let's run it. First with one word, then with two words, and finally with three words. And that's how you can use index of and substring together to let you find and split up strings.