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"
Principle 7.6.1. DEBUGGING TIP: substring(int p1, int p2)
.
Don’t forget that the second parameter in the substring()
methods refers to the character just past the last character in the substring.
Exercises Self-Study Exercises
1. Substring expressions.
2. Substring expressions 2.