Student: Hey prof, I had a program where someone typed in email addresses, and I wanted to get the part before the
@. Are there a bunch of String. methods like there are Math. methods?Prof: Clever to ask it that way. There are a bunch of methods that do work on strings. Using
String methods look a little more like what we’ve done for Pict.Prof: There are a lot of string methods defined by Java, and they are used quite frequently.
Student: OK, that’s helpful. What does the
6 for atIndex mean?Prof: The
indexOf method returns the index where it finds the string. We say each character in the string is at an index, which starts at 0 and counts from the beginning of the string. In the example b is at index 0, o at index 1, c at index 2, and so on until the @ at index 6.Student: Got it. What are some common things people do with strings?
Prof: There are a few that jump to mind. One category is searching: checking if a string appears in another, in different ways. Like is it anywhere in the string, at the start, at the end, and so on:
Student: I see. These return
false when it’s not found – you used indexOf above to find the @ – what happens if you search for something that isn’t there with indexOf?Prof: Great question, you can try yourself!
Student: Oh, right! Well I can use your example and search for something that isn’t there, like an exclamation mark.
Student: Huh, it returned
-1. I guess otherwise it returns where in the string it found it?Prof: Nice work. Another category of string operations are for transforming the string and giving a new one back:
Student: OK, so there’s a lot we can do
Prof: Yup. Let’s try to put it into practice a bit. One thing we often want to do with strings is case-insensitive search. That is, you might search a course catalog for all the courses whose titles contain
"intro", but really you want it to match "Intro" or "intro". A trick that’s often used there is to lowercase both the search term and the target string, and then use something like contains or indexOf:Student: OK, I see!
Practice
A bookstore’s search box matches the beginning of a title, and it should not care what case the shopper types. Fill in the body of
titleStartsWith so the four calls below come out as their names say.record Book(String title, double price) {
boolean titleStartsWith(String search) {
}
}
Book hobbit = new Book("The Hobbit", 12.99);
Book howTo = new Book("How to Code in Java", 40.0);
boolean hobbitFound = hobbit.titleStartsWith("the h");
boolean howToFound = howTo.titleStartsWith("HOW TO");
boolean hobbitMissed = hobbit.titleStartsWith("hobbit");
boolean howToMissed = howTo.titleStartsWith("java");
boolean titleStartsWith(String search) {
}
}
Book hobbit = new Book("The Hobbit", 12.99);
Book howTo = new Book("How to Code in Java", 40.0);
boolean hobbitFound = hobbit.titleStartsWith("the h");
boolean howToFound = howTo.titleStartsWith("HOW TO");
boolean hobbitMissed = hobbit.titleStartsWith("hobbit");
boolean howToMissed = howTo.titleStartsWith("java");