commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alex Chaffee / Purple Technology <>
Subject Re: [lang] StringUtils Questions and Suggestions
Date Wed, 05 Mar 2003 20:15:15 GMT
On Wed, Mar 05, 2003 at 02:02:17PM -0500, Henri Yandell wrote:
> > * It would be great if many the methods could be written to work on a
> >   PrintWriter as well as just creating and returning a string
> Ditto for char[] and StringBuffer. Problem is, how to implement this.
> We either end up with 3 bad performance and 1 good performant class, or we
> have lots of redundant code.

My gut says that the real performance loss is in copying strings
around, so anything we do should endeavor to write the characters once
only.  With that in mind, I suggest we unify implementations to use a
PrintWriter.  Then the clients that want a String can use
StringPrintWriter; the ones that want a char[] can use

If there's a client who wants to append to an existing StringBuffer,
then maybe we can write a simple subclass of PrintWriter that accepts
an existing StringBuffer and appends onto it.  Ditto for writing to an
existing char[] (though I've never seen this case in the wild).

That way there's no implementation bifurcation: We would provide
helper methods that wrap these core PrintWriter methods by making a
fresh new StringPrintWriter() and returning its output.

(Oh yeah, I want to submit StringPrintWriter too.  It turns this:

	StringWriter sout = new StringWriter();
	PrintWriter pout = new PrintWriter(sout);
	pout.flush();             // must flush one...
	return sout.toString();   // and return the other

into this:

	StringPrintWriter out = new StringPrintWriter();
	return out.getString();

Where would that one go?  Note that it's a trivial class:

> > * Um, what's the difference between mid() and substring()?
> It seems to just be that you specify length in mid. Bit too much of
> overkill I agree.

At least mid() should call substring() to avoid code duplication.

> > * And why isn't strip*() called trim*()?
> trim implies whitespace, whereas strip is any character. That was my only
> reason for not naming it the same originally.

It's a bit confusing.  Not a huge problem.  My first impression of the
difference was that strip would work on the whole string, where trim
works on the edges; that turned out not to be so.

Alex Chaffee                     
Purple Technology - Code and Consulting
jGuru - Java News and FAQs       
Gamelan - the Original Java site 
Stinky - Art and Angst           

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message