Presents your JAVA E-NEWSLETTER for January 16, 2003 <-------------------------------------------> MANIPULATE EXPRESSIONS WITH THE NEW FEATURES OF JAVA.LANG.STRING The Java String class has remained largely unchanged since JDK 1.0, receiving only a minor addition of new methods with JDK 1.2. However, there have been some major additions in JDK 1.4. Regular expressions have arrived with much fanfare in JDK 1.4, but the melding of the java.lang.String class to the java.util.regexp package has been less talked about. Four new regexp-based methods have arrived, with their various overloads, to help enhance the String class. The String.matches(String) method returns true if the current String matches a given regular expression. For example: "Music".matches("M.*") returns true while "Noise".matches("M.*") returns false. The String.replaceFirst(String, String) method replaces the first instance of a regular expression with a replacement value and returns the new version of the String. For example: "Small angry, angry kittens".replaceFirst("angry", "fluffy") will give us: "Small fluffy, angry kittens". We also have the replaceAll method, which is exactly the same except that it will replace all the values. So we get: "Small fluffy, fluffy kittens". The first argument to a replace method is a regular expression, while the second argument is the replacement value. This replacement value may contain references to captured values. Lastly, we have the String.split(String) method, which turns a String into an array of Strings, based on a common delimiter. The following code shows how to split a line of comma-separated values: String csv = "one,two, three,four, five"; String[] fields = csv.split(",\s*"); The argument may be a regular expression, which allows the code in this instance to ignore the white space characters. The addition of regular expressions to Java is a long-awaited affair, but the new helper methods in java.lang.String are an added bonus that should reduce the regular expression learning curve. ----------------------------------------