Advanced String Handling

There are several other 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 use on other objects later.

Comparing Two Strings

One thing you are 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 = "ukulele";
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, stores the name of Ada’s favorite instrument: a piano. The other, guess, stores a guess as to what her favorite might be. The guess is that Ada prefers the ukulele.

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 makes use of 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 has the same value as another. If the two string variables have the same value, the text true is displayed. If not, the text false is displayed. The following is the output of this example:

Output


Is Ada's favorite instrument a ukulele?
Answer: false


Determining the Length of a String

It also can be useful to determine the length of a string in characters. You do this with 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 stores this count in 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 refer to the same thing, most computers would disagree. 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 or all lowercase letters, toUpperCase() and toLowerCase(), respectively. 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 letters—”HARVEY KEITEL”. 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 is still 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. 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 4.

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


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.


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 equals the position at which the text “you have shamed those trunks” begins. Otherwise, it will equal -1.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.31.163