Skip to main content
Logo image

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

Section 7.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.

Subsection 7.6.1 charAt() and subString() methods

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:
public char charAt(int index)
public String substring(int startIndex)
public String substring(int startIndex, int endIndex)
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 substring 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”:
String str = "HelloWorld";
str.substring(5)            ==> "World"
str.substring(3)            ==> "loWorld"
The substring(int, int) version requires that you specify both the starting and ending index of the substring. The second index always points to the character that is one beyond the last character in the String you want to retrieve. For example,
//   INDEX:   0123456789
String str = "HelloWorld";
str.substring(5,7)               ==> "Wo"
str.substring(0,5)               ==> "Hello"
str.substring(5, str.length())   ==> "World"
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 substring beginning at index 5 and extending to the end of the string. This is equivalent to str.substring(5):
//    INDEX:   0123456789
 String str = "HelloWorld";
 str.substring(5, str.length())   ==> "World"
 str.substring(5)                 ==> "World"
The fact that the second parameter in substring() refers to the character 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 the string “substring1:substring2,” in which the delimiter is the colon, ':'. The following code retrieves the substring preceding the delimiter:
String str = "substring1:substring2";
int n = str.indexOf(':');
str.substring(0,n)               ==> "substring1"
Thus, by making the second index of substring() refer to the character 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:
String str = "substring1:substring2";
str.substring(0,str.indexOf(':'))  ==> "substring1"

Exercises Self-Study Exercises

1. Substring expressions.
Given this String declaration
String s = "abcdefghijklmnopqrstuvwxyz";
evaluate each of the following expressions.
(Note: All your answers should be enclosed within "" marks.)
  1. s.substring(20)
  2. s.substring(1, 5)
  3. s.substring(23)
  4. s.substring(23, 25)
  5. s.substring(s.indexOf('x'))
2. Substring expressions 2.
Given this String declaration
String s = "abcdefghijklmnopqrstuvwxyz";
evaluate each of the following expressions.
(Note: All your answers should be enclosed within "" marks.)
  1. s.substring(20, s.length())
  2. s.substring(s.indexOf('b'), s.indexOf('f'))
  3. s.substring(s.indexOf("xy"))
  4. s.substring(s.indexOf(s.charAt(23)))
  5. s.substring(s.length() - 3)
You have attempted of activities on this page.