Re: StringBuilder Difficulties
In article <c4tk07dv1pq7u8k32qd6b7o1brc2rj8r30@4ax.com>,
Gene Wirchenko <genew@ocis.net> wrote:
Dear Java'ers:
I am working with StringBuilder now. I grant that it is faster
in execution, but it is taking a bunch of my time to get it straight.
I decided to change my VRString class (call-by-value-result) to
VRStringB. Complications ensued.
The amount of ornamentation required in my code was nasty, so I
did some simplifying.
How does one assign a String value to a StringBuilder variable?
For the class below (before I defined .Set()), I needed
cParsedWord.Value.replace(
0,cParsedWord.Value.length(),cScan.substring(xStart,xEnd));
With .Set(), I just need
cParsedWord.Value.Set(cScan.substring(xStart,xEnd));
For value equality to a String value, one needs something like
cParsedWord.Value.toString().equals("some value")
(because without the .toString(), the comparison will fail) whereas
with .equals() below, this will do it
cParsedWord.equals("some value")
***** Start of Code *****
// VRStringB Class
// StringBuilder Value-Result Parameter Handling
// Last Modification: 2011-06-28
class VRStringB
{
StringBuilder Value;
VRStringB()
{
this.Value=new StringBuilder("");
}
VRStringB
(
StringBuilder Init
)
{
this.Value=Init;
}
boolean equals
(
String theString
)
{
return this.Value.toString().equals(theString);
}
void Set
(
String theString
)
{
this.Value.replace(0,this.Value.length(),theString);
}
}
***** End of Code *****
Am I missing something about StringBuilder, or is it really this
difficult to play with? It would make a lot more sense to me if
StringBuilder worked more like String does.
Well ....
If all you need is something that's like a String but whose value
can change, it seems to me [*] that you might be better off just
writing a simple wrapper class for String -- I'm thinking a class
with one variable of type String, which could even be public if
you don't want to fool with writing getter/setter methods.
[*] The experts may disagree. I'm a journey(wo)man at best
with Java.
Where StringBuilder is useful is in, well, building strings;
a typical use case is a situation in which you want to build
up a string piece by piece. You *could* write something like
String a = "first";
a += " second";
a += " etc";
and my *guess* is that this is not horribly inefficient if the
number of concatenation operations is small. (The conventional
wisdom, as I understand it, is that the Java runtime is pretty
good at managing short-lived objects, so creating new objects is
not invariably something to avoid, though as with anything else
one shouldn't get carried away, maybe. Again the experts may
disagree.)
But if there are a lot of concatenation operations it's said to be
more efficient to use a StringBuilder, e.g.:
StringBuilder sb = new StringBuilder();
sb.append("first");
sb.append(" second");
sb.append(" etc");
String a = sb.toString();
The above is what I mostly use StringBuilder for; there are
undoubtedly other things one can do with it as well, some of
which may be useful to you (I haven't followed carefully all the
threads you've started).
(I'm not optimistic that this commentary will be helpful, or
even that it will be read [*], given that you didn't reply to
my posts in the threads about passing method names to methods,
but I guess I'll try again .... <shrug> )
[*] That's not actually meant as snark; not long ago I changed
the e-mail address I use to post, to a GMail one, and I'm under
the impression that some Usenet participants routinely filter out
anything from a GMail address, so I worry just a bit ....
--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.