Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2024E

Section 7.3 Finding Things Within a String

Programmers often have to find the location of a particular character or substring in a string. For example, user names and passwords are sometimes stored in a single string in which the name and password are separated from each other by a special character, such as a colon (username:password). In order to get the name or password from such a string, it is convenient to have methods that will search the string and report the index of the colon character.

Subsection 7.3.1 indexOf()

The indexOf() and lastIndexOf() methods are instance methods that can be used to find the index position of a character or a substring within a String. There are several versions of each:
public int indexOf(int character);
public int indexOf(int character, int startingIndex);
public int indexOf(String string);
public int indexOf(String string, int startingIndex);
public int lastIndexOf(int character);
public int lastIndexOf(int character, int startingIndex);
public int lastIndexOf(String string);
public int lastIndexOf(String string, int startingIndex);
The indexOf() method searches from left to right within a String for either a character or a substring. The lastIndexOf() method searches from right to left for a character or substring. To illustrate, suppose we have declared the following strings:
String string1 = "";
String string2 = "Hello";
String string3 = "World";
String string4 = string2 + " " + string3;
Recalling that strings are indexed starting at 0, searching for o in the various strings gives the following results:
string1.indexOf('o') ==> -1  string1.lastIndexOf('o') ==> -1
string2.indexOf('o') ==>  4  string2.lastIndexOf('o') ==>  4
string3.indexOf('o') ==>  1  string3.lastIndexOf('o') ==>  1
string4.indexOf('o') ==>  4  string4.lastIndexOf('o') ==>  7
Because string1 is the empty string, “”, it does not contain the letter o. Therefore, indexOf() returns \(-1\text{,}\) a value that cannot be a valid index for a String. This convention is followed in indexOf() and lastIndexOf().
Because string2 and string3 each contain only one occurrence of the letter o, both indexOf() and lastIndexOf() return the same value when used on these strings. Because string4 contains two occurrences of o, indexOf() and lastIndexOf() return different values in this case. As Figure 7.3.1 shows, the first o in “Hello World” occurs at index 4, the value returned by indexOf(). The second o occurs at index 7, which is the value returned by lastIndexOf().
Figure 7.3.1. Indexing of “Hello World”.
By default, the single-parameter versions of indexOf() and lastIndexOf() start their searches at their respective (left or right) ends of the string. The two-parameter versions of these methods allow you to specify both the direction and starting point of the search. The second parameter specifies the starting index. Consider these examples:
string4.indexOf('o', 5)     ==> 7
string4.lastIndexOf('o', 5) ==> 4
If we start searching in both cases at index 5, then indexOf() will miss the o that occurs at index 4. The first o it finds will be the one at index 7. Similarly, lastIndexOf() will miss the o that occurs at index 7 and will find the o that occurs at index 4.
The indexOf() and lastIndexOf() methods can also be used to find substrings:
string1.indexOf("or") ==> -1  string1.lastIndexOf("or") ==> -1
string2.indexOf("or") ==> -1  string2.lastIndexOf("or") ==> -1
string3.indexOf("or") ==>  1  string3.lastIndexOf("or") ==>  1
string4.indexOf("or") ==>  7  string4.lastIndexOf("or") ==>  7
The substring “or” does not occur in either string1 or string2. It does occur beginning at location 1 in string3 and beginning at location 7 in string4. For this collection of examples, it doesn’t matter whether we search from left to right or right to left.

Exercises Self-Study Exercises

1. String indexOf() expressions.
Suppose the String variable s has been initialized to “mom.” Evaluate each of the following expressions:
  1. s.indexOf("m");
  2. s.indexOf("o");
  3. s.indexOf("M");
Hint.
The indecOf() method returns the index of its argument’s location in the string.
2. String indexOf() expressions.
Suppose the String variable s has been initialized to: String s1 = "Java, Java, Java"; Evaluate each of the following expressions:
  1. s1.length()
  2. String.valueOf(s1.length())
  3. s1.indexOf('a')
  4. s1.lastIndexOf('a')
3. String indexOf() expressions.
Suppose the String variable s has been initialized to: String s1 = "Java, Java, Java"; Evaluate each of the following expressions:
  1. s1.indexOf("av")
  2. s1.lastIndexOf("av")
  3. s1.indexOf('a', 5)
  4. s1.lastIndexOf('a', 5)
4. String indexOf() expressions.
Suppose the String variable s has been initialized to: String s1 = "Java, Java, Java"; Evaluate each of the following expressions:
  1. s1.indexOf("av", s1.length() - 10)
  2. s1.lastIndexOf("av", s1.length() - 4)
  3. s1.indexOf("a", s1.indexOf("va"))
5. Tricky indexOf() expression.
Evaluate the following expression:
String tricky = "abcdefg01234567";
tricky.indexOf(String.valueOf( tricky.indexOf("c") ));
Hint.
Evaluate the expressions starting with the innermost parentheses.
You have attempted of activities on this page.