Manipulating Strings in Java
Much of what you do in any programming language involves the manipulation of strings. Other than numeric data, nearly all data is accessed as a string. Quite often, even numeric data is treated as a simple string. It is difficult to imagine being able to write a complete program without making use of strings.
The phrases in this chapter show you some common tasks involving strings. The Java language has strong built-in support for strings and string processing. Unlike the C language, strings are built-in types in the Java language. Java contains a String class that is used to hold string data. Strings in Java should not be thought of as an array of characters as they are in C. Whenever you want to represent a string in Java, you should use the String class, not an array.
An important property of the String class in Java is that once created, the string is immutable. This means that once created, a Java String object cannot be changed. You can reassign the name you’ve given a string to another string object, but you cannot change the string’s contents. Because of this, you will not find any set methods in the String class. If you want to create a string that you can add data to, such as you might in some routine that builds up a string, you should use the StringBuilder class if you are using JDK 1.5, or the StringBuffer class in older versions of Java, instead of the String class. The StringBuilder and StringBuffer classes are mutable; thus you are allowed to change their contents. It is very common to build strings using the StringBuilder or StringBuffer class and to pass or store strings using the String class.
Comparing Strings
boolean result = str1.equals(str2); boolean result2 = str1.equalsIgnoreCase(str2);
The value of result and result2 will be true if the strings contain the same content. If the strings contain different content, the value of result and result2 will be false. The first method, equals(), is case sensitive. The second method, equalsIgnoreCase(), will ignore the case of the strings and return true if the content is the same regardless of case.
String comparison is a common source of bugs for novice Java programmers. A novice programmer will often attempt to compare strings using the comparison operator ==. When used with Strings, the comparison operator == compares object references, not the contents of the object. Because of this, two string objects that contain the same string data, but are physically distinct string object instances, will not compare as equal when using the comparison operator.
The equals() method on the String class compares a string’s contents, rather than its object reference. This is the preferred string comparison behavior in most string comparison cases. See the following example:
String name1 = new String("Timmy"); String name2 = new String("Timmy"); if (name1 == name2) { System.out.println("The strings are equal."); } else { System.out.println("The strings are not equal."); }
The output from executing these statements will be
The strings are not equal.
Now use the equals() method and see the results:
String name1 = new String("Timmy"); String name2 = new String("Timmy"); if (name1.equals(name2)) { System.out.println("The strings are equal."); } else { System.out.println("The strings are not equal."); }
The output from executing these statements will be
The strings are equal.
Another related method on the String class is the compareTo() method. The compareTo() method compares two strings lexographically, returning an integer value—either positive, negative, or 0. The value 0 is returned only if the equals() method would evaluate to true for the two strings. A negative value is returned if the string on which the method is called alphabetically precedes the string that is passed as a parameter to the method. A positive value is returned if the string on which the method is called alphabetically comes after the string that is passed as a parameter. To be precise, the comparison is based on the Unicode value of each character in the strings being compared. The compareTo() method also has a corresponding compareToIgnoreCase() method that performs functionally the same with the exception that the characters’ case is ignored. See the following example:
String name1="Camden"; String name2="Kerry"; int result = name1.compareTo(name2); if (result == 0) { System.out.println("The names are equal."); } else if (result > 0) { System.out.println( "name2 comes before name1 alphabetically."); } else if (result < 0) { System.out.println( "name1 comes before name2 alphabetically."); }
The output of this code will be
name1 comes before name2 alphabetically.