Java Reference
In-Depth Information
How It Works
You should have no trouble with this example. It declares and initializes three String variables,
string1 , string2 , and string3 . We then create three further String variables that correspond to
the first three strings with double quote characters at the beginning and the end. This is just to simplify
the output statements. We then have an if with a nested if to compare string1 with string3 . We
compare string2 with string1 in the same way.
As with the equals() method, the argument to the method compareTo() can be any expression that
results in a String object.
Accessing String Characters
When you are processing strings, sooner or later you will need to access individual characters in a
String object. To refer to a character at a particular position in a string you use an index of type int
that is the offset of the character position from the beginning of the string. This is exactly the same
principle as we used for referencing an array element. The first character in a string is at position 0, the
second is at position 1, the third is at position 2, and so on. However, although the principle is the same,
the practice is not. You can't use square brackets to access characters in a string - you must use a
method.
Extracting String Characters
You can extract a character from a String object by using the method charAt() . This accepts an
argument that is the offset of the character position from the beginning of the string - in other words, an
index. If you attempt to use an index that is less than 0 or greater than the index for the last position in
the string, you will cause an exception to be thrown, which will cause your program to be terminated.
We will discuss exactly what exceptions are, and how you should deal with them, in Chapter 7. For the
moment, just note that the specific type of exception thrown in this case is called
StringIndexOutOfBoundsException . It's rather a mouthful, but quite explanatory.
To avoid unnecessary errors of this kind, you obviously need to be able to determine the length of a
String object. To obtain the length of a string, you just need to call its length() method. Note that
this is different from the way you got the length of an array. Here you are calling a method, length() ,
in the class String , whereas with an array you were accessing a data member, length . We can
explore the use of the charAt() and length() methods in the String class with another example.
Try It Out - Getting at Characters in a String
In the following code the soliloquy is analyzed character-by-character to determine the vowels, spaces,
and letters used.
public class StringCharacters {
public static void main(String[] args) {
// Text string to be analyzed
String text = "To be or not to be, that is the question;"
+"Whether 'tis nobler in the mind to suffer"
+" the slings and arrows of outrageous fortune,"
+" or to take arms against a sea of troubles,"
+" and by opposing end them?";
Search WWH ::




Custom Search