Section 7.3. Finding Things within a String


[Page 313 (continued)]

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.

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 a 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 



[Page 314]

Because string1 is the empty string, "", it does not contain the letter o. Therefore, in-dexOf() returns -1, 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.7 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.7. The indexing of the "Hello World" string.


Sentinel return value


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.

Self-Study Exercises

Exercise 7.7

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");


[Page 315]
Exercise 7.8

Evaluate the expressions given the String declaration

String s1 = "Java, Java, Java";


  1. s1.length()

  2. String.valueOf(s1.length())

  3. s1.indexOf('a')

  4. s1.lastIndexOf('a')

  5. s1.indexOf("av")

  6. s1.lastIndexOf("av")

  7. s1.indexOf('a', 5)

  8. s1.lastIndexOf('a', 5)

  9. s1.indexOf("av", s1.length() - 10)

  10. s1.lastIndexOf("av", s1.length() - 4)

  11. s1.indexOf("a", s1.indexOf("va"))

Exercise 7.9

Evaluate the following expression:

String tricky = "abcdefg01234567"; tricky.indexOf(String.valueOf( tricky.indexOf("c"))); 





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net