Page 201 - Introduction to Programming with Java: A Problem Solving Approach
P. 201
5.6 String Methods 167 That statement compiles and runs, but it does not change middleInitial’s content. Here’s the proper way
to do it:
middleInitial = Character.toUpperCase(middleInitial);
The IdentifierChecker program in Figure 5.7 illustrates the character class in the context of a com- plete program. It uses the Character class’s isLetter and isLetterOrDigit methods to check whether the user entry is a legal identifier.
5.6 String Methods
The String class is another one of the classes in the always-available java.lang package. In Chap- ter 3 you saw several examples of useful methods associated with objects of the String class such as the charAt method, the length method, the equals method, and the equalsIgnoreCase method. In this section, we describe some additional String methods—the String methods shown in Figure 5.8. These String methods do not have the static access modifier, so they are not class methods, and you cannot access them with the class name. They are instance methods and you must access them with a par- ticular string instance. Or said another way, you must access them with a calling-object string.
Lexicographical Ordering of Strings
You know that numbers can be compared to determine which number is greater. Strings can also be com-
pared. When computers compare strings to determine which string is greater, they use lexicographical or-
dering. For the most part, lexicographical ordering is the same as dictionary order. The string “hyena” is Apago PDF Enhancer
greater than the string “hedgehog” because hyena comes after hedgehog in the dictionary.
The String class’s compareTo method compares two strings to determine which is greater. As explained in Figure 5.8, compareTo returns a positive number if the calling string is greater than the argument string, a negative number if the calling string is less than the argument string, and zero if the calling string and argument string are the same. The following code fragment illustrates what we’re talking about. It compares YouTube1 video titles and prints the results of the comparisons. If you run this code fragment, don’t be surprised if your first two output values are different from 1 and 14. According to Sun’s specification, the first two output values can be any positive number and any negative number,
respectively.
String youTubeVideo = "Colbert Invades Cuba";
System.out.println(
youTubeVideo.compareTo("Bad Day at Work") + " " +
youTubeVideo.compareTo("Colbert Whitehouse Dinner") + " " +
youTubeVideo.compareTo("Colbert Invades Cuba"));
Output:
1 -14 0
Checking for the Empty String
Previously, you learned that the empty string is a string that contains no characters, and it’s represented by two quotes with nothing between them—"". Sometimes you’ll need to check a string variable to see whether
1 YouTube is a popular free video sharing Web site, acquired by Google in October, 2006, which lets users upload, view, and share video clips.