wood burning stoves 2.0*
The moose likes Java in General and the fly likes String Utility Class available somewhere??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Utility Class available somewhere???" Watch "String Utility Class available somewhere???" New topic
Author

String Utility Class available somewhere???

Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
I just found out that there is nothing avalailble in the core language, especially the String class to pad a string (to the right, or left, or centered) with a number of characters (be it blank, zero, etc...).
Is there a free package or class somewhere that already does that type of work?
Thanks in advance!
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Serge: I don't off hand know of a third party package to do what you want. (There probably are some, though.) Keep in mind that Java contains other String-related classes, including StringBuffer, which you might find helpful. Also, Java's regular expression processing capabilities (found in java.util.regex) might be of interest.
Because of the clean and elegant way that Java handles strings, it is also pretty easy to write your own methods to do the type of operations that you describe. For example, here is a very simple class that pads or centers a string. I don't know if it is precisely what you had in mind, but it might give you some ideas. (Of course, for real-world programs, additional error and boundary checking may be in order!)

The ouput from the program is shown here:
Original string: |ABCD|
Left padded: | ABCD|
Right padded: |ABCD0000000000|
Centered: | ABCD


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I recommend using StringBuffer for something like this:

Note that I've also changed the API here - minLen is the total length you want the final String to be, minimum, rather than the number of characters to pad. This has been slightly more convenient in my experience, but I suppose it depends what you're doing.
Incedentally I think it's fairly likely that JDK 1.5 will have more direct support for this when it comes out. Java has long been missing a good substitute for C's printf() function. Though NumberFormat classes do handle some of this and offer additional benefits, it's still an annoying amount of work to format a string as easily as you could in C. To "do it right" in Java they wanted to wait until they had some other features in the language, namely varargs and automboxing, and those are (supposedly) coming in 1.5 too. So there's not a great deal of incentive for anyone else to develop an extensive custom library for this right now, as we should have a good substitute soon. Where "soon" is something like "in about a year", for the non-beta release. Meanwhile, we just roll our on solutions, as shown above.


"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The REXX language has left() right() and center() routines much like those described above but they also truncate strings that are too long. This helps write columnar reports to stdout, and lets you get the first or last "n" characters of a string very easily. I have rewritten a dozen or so REXX functions in nearly every language I've used since 1980. Good things to have around! So if you're wondering about writing your own, it's a fine thing to do and to keep around for future use. You might save a little work by finding pre-baked methods, but you learn from writing your own. Have fun!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Serge: The key difference between Jim's approach and mine is that Jim uses the fact that a StringBuffer holds a mutable (i.e., changeable) string and can, therefore, be padded "in place" through the use of append(). By contrast, the String class holds immutable (i.e., unchangeable) strings. My approach works through the use of the string concatenation operator + to build a new string. In the process, it creates temporary objects which will ultimately be garbage collected. This is why Jim likes to use StringBuffer; it avoids the creation of the temporary objects. Thus, the use of StringBuffer can be more efficient even though it requires a few more lines of code.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Stan - good point. Last time I needed one of these it was a context where letting the field overrun was preferable to truncating it; obviously that's not always the case. And sometimes instead of truncating, you'd rather just return "******" or some such to indicate the overflow. (Probably covered in one of the other dozen junctions Stan mentions.)
Herb's explanation of why I prefer StringBuffer here is of course exactly right. I should have contextualized it a bit more - there's nothing wrong with Herb's solution, given that he said he was providing a really simple solution; it's all a question of how much time you want to spend on these things. Personally I just automatically think of StringBuffer the moment I want to concatenate to a String inside a loop of any kind; outside a loop it's probably not worth bothering with (and may even be counterproductive if you accidentally break up a compile-time constant).
For the original question: I just did a little digging, and it seems that Jakarta has a StringUtils class as part of Commons-Lang. I haven't used it myself, but it's probably a decent choice for a ready-made library.
 
 
subject: String Utility Class available somewhere???