Advanced String Handling
In addition to creating strings, pasting them together, and using them with other types of variables, there are several different ways you can examine a string variable and change its value. These advanced features are possible because strings are objects in the Java language. Working with strings develops skills you'll be using to work with other objects later.
Comparing Two Strings
One thing you will be testing often in your programs is whether one string is equal to another. You do this by using equals() in a statement with both of the strings, as in this example:
String favorite = "piano"; String guess = "ukelele"; System.out.println("Is Ada's favorite instrument a " + guess + "?"); System.out.println("Answer: " + favorite.equals(guess));
This example uses two different string variables. One, favorite, is used to store the name of Ada's favorite instrument: a piano. The other, guess, is used to store a guess as to what her favorite might be. The guess is that Ada prefers the ukelele.
The third line displays the text Is Ada's favorite instrument a followed by the value of the guess variable, and then a question mark. The fourth line displays the text Answer: and then contains something new:
favorite.equals(guess)
This part of the statement is known as a method. A method is a way to accomplish a task in a Java program. This method's task is to determine if one string, favorite, has the same value as another string, guess. If the two string variables have the same value, the text true will be displayed. If not, the text false will be displayed. The following is the output of this example:
Is Ada's favorite instrument a ukelele? Answer: false
Determining the Length of a String
It can also be useful at times to determine the length of a string in characters. You do this by using the length() method. This method works in the same fashion as the equals() method, except that only one string variable is involved. Look at the following example:
String cinematographer = "Stuart Dryburgh"; int nameLength = cinematographer.length();
This example sets nameLength, an integer variable, equal to 15. The cinematographer.length() method counts the number of characters in the string variable called cinematographer, and this count is assigned to the nameLength integer variable.
Changing a String's Case
Because computers take everything literally, it's easy to confuse them. Although a human would recognize that the text Harvey Keitel and the text HARVEY KEITEL are referring to the same thing, most computers would disagree. For instance, the equals() method discussed previously in this hour would state authoritatively that Harvey Keitel is not equal to HARVEY KEITEL.
To get around some of these obstacles, Java has methods that display a string variable as all uppercase letters (toUpperCase()) or all lowercase letters (toLowerCase()). The following example shows the toUpperCase() method in action:
String baines = "Harvey Keitel"; String change = baines.toUpperCase();
This code sets the string variable change equal to the baines string variable converted to all uppercase lettersHARVEY KEITEL, in other words. The toLowerCase() method works in the same fashion but returns an all-lowercase string value.
Note that the toUpperCase() method does not change the case of the string variable it is called on. In the preceding example, the baines variable will still be equal to Harvey Keitel.
Looking for a String
Another common task when handling strings is to see whether one string can be found inside another. This is useful when you're looking for specific text in a large number of strings (or a very large string).
To look inside a string, use its indexOf() method. Put the string you are looking for inside the parentheses. If the string is not found, indexOf() produces the value -1. If the string is found, indexOf() produces an integer that represents the position where the string begins. Positions in a string are numbered upwards from 0, beginning with the first character in the string. (In the string "The Piano", the text "Piano" begins at position 3.)
One possible use of the indexOf() method would be to search the entire script of The Piano for the place where Ada's domineering husband tells her daughter Flora, "You are greatly shamed and you have shamed those trunks."
If the entire script of The Piano was stored in a string called script, you could search it for part of that quote with the following statement.
int position = script.indexOf("you have shamed those trunks");
If that text can be found in the script string, position will equal the position at which the text "you have shamed those trunks" begins. Otherwise, it will equal -1.
CAUTION
The indexOf() method is case sensitive, which means that it only looks for text capitalized exactly like the search string. If the string contains the same text capitalized differently, indexOf() produces the value -1.