10. Strings and String Processing

10.6. Retrieving Parts of Strings

 

Programmers often need to retrieve an individual character or a part of a string from a string, as, for example, in a word processing program when a part of a string is copied or deleted. In this section we look at methods that help us with these kinds of tasks.

The charAt(int index) method is a String instance method that can be used to retrieve the character stored at a certain index. The several varieties of the substring() method can be used to retrieve a substring of characters from a String. These methods are defined as follows:

,,

 

 

 

J

The charAt() method returns the character located at the index supplied as its parameter. Thus, str.charAt(0) retrieves the first character in str, while str.charAt(str.length()-1) retrieves the last character. The substring() methods work in a similar way, except that you need to specify both the starting and the ending index of the sub- string you wish to retrieve.The first version of substring(int startIndex) takes a single parameter and returns a String consisting of all the characters beginning with startIndex and continuing up to the end of the String. For example, if the str is “HelloWorld”, then str.substring(5) would return “World” and str.substring(3)

would return “loWorld”:

,,

 

 

J

The substring(int, int) version requires that you specify both the starting and ending index of the substring. The second index always

 

SECTION 7.6 Retrieving Parts of Strings311

points to the character that is one beyond the last character in the String you want to retrieve. For example,

 

 

 

 

 

\J

Note here that when we want to retrieve “Wo” from str, we specify its substring as indexes 5 and 7; the 7 points to the character just beyond “Wo.” Similarly, substring(0,5), picks out the first five characters (“Hello”). In the third example, the length() method specifies the sub- string beginning at index 5 and extending to the end of the string. This is equivalent to str.substring(5):

 

 

 

 

\J

The fact that the second parameter in substring() refers to the char- acter one beyond the desired substring may seem a bit confusing at first, but it is actually a very useful way to designate a substring. For example, many string-processing problems have to do with retrieving substrings from a delimited string, which is a string that contains special characters

that separate the string into certain substrings. For example, consider theDelimited strings

string “substring1:substring2,” in which the delimiter is the colon, ’:’. The following code retrieves the substring preceding the delimiter:

,,

 

 

J

Thus, by making the second index of substring() refer to the char- acter one beyond the last character in the desired substring, we can use indexOf() and substring() together to process delimited strings. Note that it is not necessary to use a temporary variable n to store the index of the delimiter, because the two method calls can be nested:

,,

 

J

 

SELF-STUDY EXERCISES

 

EXERCISE 7.10Given the String declaration

,,

 

J

evaluate each of the following expressions:

 

s.substring(20)

s.substring(1, 5)

s.substring(23)


s.substring(23, 25)

s.substring(s.indexOf(’x’))

 

EXERCISE 7.11Given the preceding declaration of s, evaluate each of the following expressions:

s.substring(20, s.length())

s.substring(s.indexOf(’b’), s.indexOf(’f’))

s.substring(s.indexOf("xy"))

s.substring(s.indexOf(s.charAt(23)))

s.substring(s.length() - 3)