Chapter 2. Manipulating Text

Introduction

Parsing text fell out of fashion when Extensible Markup Language (XML) came to town. Java’s text manipulation capabilities have only recently received some badly overdue attention, with the addition of regular expressions and numerous improvements in Java 1.5. Despite the improvements, using Java to manipulate plain text can inspire frustration. Hand a developer a fixed-length text file format to parse, and you will hear the groans—“Why can’t we use something `modern’ like XML? I don’t want to parse text.” And while parsing and manipulating text may not build you a sexy resume, there is still a need to work with text in any program. For every snazzy XML file format, there is a widely accepted text file format with fixed-length fields; and for every XML web service, a vendor sends a proprietary feed that must be parsed character by character. Text manipulation is a must, but the Java 2 Standard Edition (J2SE) leaves developers wanting for more convenient ways to slice, dice, chomp, and manipulate text. This chapter introduces some simple utilities, which fill a few of Java’s many gaps.

The recipes in this chapter deal with a collection of static methods on two classes in Jakarta Commons Lang—StringUtils and WordUtils. Some of the methods on StringUtils have been made obsolete by changes introduced in Java 1.4 and Java 1.5, but, regardless, there are times when you will want to split a string without regular expressions. Using StringUtils.chomp( ...

Get Jakarta Commons Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.