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( ) achieves more readable logic than writing the equivalent logic using the String.indexOf( ) and String.substring() methods. These recipes will not change the basic architecture of a system with new, groundbreaking technology, but they are valuable “shortcuts” whose benefits will add up over time. Simple solutions to common tasks will save minutes of coding and hours of maintenance in the long run. Over time, many grow so accustomed to the efficiencies gained with Commons Lang, they consider it an indispensable supplement to the Java language.

Read the recipes in this chapter, and take some time to read the Javadoc for StringUtils and WordUtils. These utility classes provide some common string operations missing in Java, and they are simple and well-documented. These utilities are so simple that, throughout this chapter, you will notice recipes without a Discussion or See Also section—it’s just not needed! These simple recipes have relevance to all skill levels; even if you are an experienced developer, you may be surprised at what Commons Lang string manipulation has to offer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.132.99