Other Important String Class MethodsThe String class has other methods that will be useful to you in your scientific and engineering programming. These methods can be used to compare, split, trim, or determine the length of Strings . The syntax of some of the more important methods is ” compareTo() compares two Strings lexicographically based on the Unicode value of each character. The method returns a negative integer if the String on which the method is invoked lexicographically precedes the argument, a positive integer if it follows the argument String , and zero if the Strings are equal. This method can be used to sort a collection of Strings and throws a NullPointerException if the argument is null . compareToIgnoreCase() does the same thing as compareTo() but case is ignored. equals() returns true if the Object argument is a String and has the same character sequence as the String on which the method is invoked. equalsIgnoreCase() does the same thing as equals() but case is ignored. length() returns the number of 16-bit Unicode characters in a String . split() splits a String into substrings around matches of the specified regular expression. The regular expression can be a single character or a series of characters. If a limit argument is specified, this is the maximum number of times the regular expression will be applied. A negative limit value means the regular expression will be matched as many times as possible. A limit value of 0 matches the regular expression as many times as possible and any empty substrings are discarded. This method can throw a PatternSyntaxException if the regex syntax is invalid or a NullPointerException if regex is null. substring() returns a substring of a String . The substring is extracted between beginIndex and endIndex of the original String . If endIndex is not specified, the substring will continue to the end of the original String . This method can throw an IndexOutOfBoundsException if a bad index value is passed to the method. trim() returns a String with leading and trailing white space removed. Example: Using String Class MethodsA comparator is a class that implements the Comparator interface. A comparator class overrides the compare() method to provide a comparison of two objects. In this example, the LexComparator class will implement the compare() method to lexicographically compare two String objects using the compareTo() method from the String class. The compareTo() method returns a positive integer if str1 is lexicographically greater than str2 , a negative integer if it is lexicographically less, and zero if the two Strings are the same. import java.util.Comparator; public class LexComparator implements Comparator { public int compare(Object obj1, Object obj2) { String str1 = (String)obj1; String str2 = (String)obj2; return str1.compareTo(str2); } } The MethodsDemo class creates a TreeSet object that will be used to store the names of planets. One of the TreeSet constructors allows you to specify a Comparator that will be used to sort the elements of the TreeSet . We will use a LexComparator that will sort the elements lexicographically. The elements of the TreeSet are automatically placed in alphabetical order. Every time an element is added to the collection, the elements are resorted by the Comparator . import java.util.TreeSet; public class MethodsDemo { public static void main(String args[]) { // Create a TreeSet using a LexComparator object // to sort the elements TreeSet planets = new TreeSet(new LexComparator()); // When elements are added they are automatically // placed in alphabetical order. planets.add("Jupiter"); planets.add("Mercury"); planets.add("Earth"); System.out.println("contents = " + planets); // Every time you add an element the // collection is resorted. planets.add("Mars"); System.out.println("contents = " + planets); } } Output ” contents = [Earth, Jupiter, Mercury] contents = [Earth, Jupiter, Mars, Mercury] For more examples of using the String class methods, see the "Parsing an Input File" example at the end of this chapter where the equals() , trim() , and split() methods are used to parse String objects read from an input file. |