Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > StringBuilder Difficulties

Reply
Thread Tools

StringBuilder Difficulties

 
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-29-2011
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(xStar t,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.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-29-2011
On 6/28/2011 8:54 PM, Gene Wirchenko 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?


One does not "assign" a reference of type T1 to a reference
of unrelated type T2. One can, however, create a StringBuilder
whose initial content is the same as that of a given String:

String str = ...; // non-null
StringBuilder sbd = new StringBuilder(str);

These are "the same" in the sense that str.length() == sbc.length()
and str.charAt(k) == sbd.charAt(k) for all 0 <= k < str.length().
Also, str.equals(sbd.toString()) returns true.

> [...]
> Am I missing something about StringBuilder, or is it really this
> difficult to play with?


You've been advised to read some Java tutorials or textbooks,
and (it seems) have chosen not to follow that advice. Under the
circumstances, then, I'd have to say StringBuilder is "really this
difficult to play with."

Perhaps you're "too pressed for time" to learn Java before writing
it. Okay, yeah, that's reality, **** happens. But if so, you've been
"pressed for time" for some weeks now, and (I'll wager) have already
wasted more time chasing blind alleys than you would have spent learning
the rudiments of the language. (A potential infinite regress looms.)

> It would make a lot more sense to me if
> StringBuilder worked more like String does.


StringBuilder is only of interest because it is mutable, while
String is immutable. From this fact flow many of the necessary
differences that give you so much trouble, but that others take in
stride.

Summary: Fer Crissakes, Gene, read the damn' book!

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      06-29-2011
Eric Sosman wrote:
> Summary: Fer Crissakes, Gene, read the damn' book!


And don't go plonking everyone who gives you good advice, either, Gene. Eric,
like markspace before him, is giving you good advice. Follow it instead of
copping an attitude like you did before.

Straight, unvarnished, blunt truth can feel bad at first, but it helps you.
It really does.

Also, refusal to learn harms you.

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-29-2011
On Tue, 28 Jun 2011 21:29:00 -0400, Eric Sosman
<(E-Mail Removed)> wrote:

>On 6/28/2011 8:54 PM, Gene Wirchenko 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?

^^^^^
> One does not "assign" a reference of type T1 to a reference

^^^^^^^^^
I wrote "value".

>of unrelated type T2. One can, however, create a StringBuilder
>whose initial content is the same as that of a given String:
>
> String str = ...; // non-null
> StringBuilder sbd = new StringBuilder(str);


I have found that, but I was wondering about how to do it with
the same StringBuilder object. I thought that part of the advantage
of using StringBuilder was that the amount of object creation got cut
down.

>These are "the same" in the sense that str.length() == sbc.length()
>and str.charAt(k) == sbd.charAt(k) for all 0 <= k < str.length().
>Also, str.equals(sbd.toString()) returns true.


IOW, a String value assigned to a StringBuilder variable.

>> [...]
>> Am I missing something about StringBuilder, or is it really this
>> difficult to play with?

>
> You've been advised to read some Java tutorials or textbooks,
>and (it seems) have chosen not to follow that advice. Under the
>circumstances, then, I'd have to say StringBuilder is "really this
>difficult to play with."


Which I have. I have been referring to the docs for
StringBuilder, but the docs are rather incomplete, and I have to
guess. This takestime.

> Perhaps you're "too pressed for time" to learn Java before writing
>it. Okay, yeah, that's reality, **** happens. But if so, you've been
>"pressed for time" for some weeks now, and (I'll wager) have already


A few and part-time.

>wasted more time chasing blind alleys than you would have spent learning
>the rudiments of the language. (A potential infinite regress looms.)


I know the rudiments. I am having trouble with the next level.

I have been asking here to try to knock out some of those blind
alleys. I can get something to work, but I would prefer to have
something work well.

>> It would make a lot more sense to me if
>> StringBuilder worked more like String does.

>
> StringBuilder is only of interest because it is mutable, while
>String is immutable. From this fact flow many of the necessary
>differences that give you so much trouble, but that others take in
>stride.


Why is the syntax so different? It does not need to be, but it
is. In order to change from String to StringBuilder, I had to modify
a bunch of code apart from changing variable types.

> Summary: Fer Crissakes, Gene, read the damn' book!


I have been RTFM. The docs often say what, but without an
example and usually not why. I am paddling the best I can. Please do
not cannonball next to me.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-29-2011
On Wed, 29 Jun 2011 00:48:40 -0400, Lew <(E-Mail Removed)> wrote:

>Eric Sosman wrote:
>> Summary: Fer Crissakes, Gene, read the damn' book!

>
>And don't go plonking everyone who gives you good advice, either, Gene. Eric,


I do not. I plonked one person who called my sanity into
question. As I like to say, disagreeing one thing, but being
disagreeable is quite another. Snarking on sanity is the latter.

>like markspace before him, is giving you good advice. Follow it instead of
>copping an attitude like you did before.
>
>Straight, unvarnished, blunt truth can feel bad at first, but it helps you.
>It really does.


The attitude of some posters is that they are Keepers of the
Truth. I suggest that there are different realities. I have
programmed in quite a few programming languages. I see no need to
jettison what I have learned, because some practice is different with
Java. If it is better, I will consider using it. If it does not meet
my needs, I see no need to use it regardless of what Java practice is.

>Also, refusal to learn harms you.


I do not refuse to learn, but I know what I want to accomplish
with my program. A number of the suggestions have been plain
irrelevant. Some of them have been quite useful, but those subthreads
tend to die, because there is no need for further discussion. Thus,
discussion tends to continue only on points of contention. They are
not the only points. There is more agreement than disagreement, but
you would not know that the way some posters keep coming at me.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
blmblm@myrealbox.com
Guest
Posts: n/a
 
      06-29-2011
In article <(E-Mail Removed)>,
Gene Wirchenko <(E-Mail Removed)> 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(xStar t,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.
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-29-2011
On Wed, 29 Jun 2011 14:38:04 -0700, Patricia Shanahan <(E-Mail Removed)>
wrote:

>On 6/28/2011 5:54 PM, Gene Wirchenko wrote:
>...
>> 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.

>...
>
>The main thing you seem to be missing about StringBuilder is that it is
>not a string, it is a tool for building strings.


Well, once I have built a string, I want to use it.

>Here is some sample code demonstrating comparing a String to the current
>contents of a StringBuilder, and replacing the entire contents with a
>different String.
>
>public class StringBuilderExample {
> public static void main(String[] args) {
> StringBuilder sb = new StringBuilder();
> sb.append("aaa");
> System.out.println(sb);
> System.out.println("aaa".equals(sb.toString()));
> sb.replace(0, Integer.MAX_VALUE, "bbbbbbb");
> System.out.println(sb);
> }
>}


I find the .replace() line rather ornamental. I have even more
complex parameters so I came up with my .Set() method. I am surprised
that there is no such functionality in StringBuilder.

I understand your example. Either I have learned, you write good
examples, or both.

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-30-2011
On Tue, 28 Jun 2011 17:54:38 -0700, Gene Wirchenko <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>
> How does one assign a String value to a StringBuilder variable?


see http://mindprod.com/jgloss/stringbuilder.html

There is a ton of source code on the net. Googling it looking for
various classes and methods can help you find how other programmers
use them which can help you make sense of the JavaDoc.

I use FastCat now for 95% of my string building work. It is aims to
use memory precisely to reduce the amount of GC. I only use
StringBuilder for character-by-character building.

see http://mindprod.com/products1.html#FASTCAT

--
Roedy Green Canadian Mind Products
http://mindprod.com
One of the curses of the computer age is manufacturers now design
home appliances to die on the very day the warranty expires.
It is deliberate waste in the service of mindless profit.
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-30-2011
On 29 Jun 2011 20:17:37 GMT, (E-Mail Removed)
<(E-Mail Removed)> wrote:

>In article <(E-Mail Removed)>,
>Gene Wirchenko <(E-Mail Removed)> wrote:


[snip]

>> 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.


[snip]

>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


IOW, who cares if it is only a bit of inefficiency? Agreed.

>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();


It is. My test code appends one character at a time. Switching
from String to StringBuilder cut the execution time by about 40%.

>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 have managed to get to the point where I have found out what
you have posted here, and I agree with it, too.

>(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 ....


Not me. I killfile only the deserving (meaning those undeserving
of my time).

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      06-30-2011
On Wed, 29 Jun 2011 15:52:46 -0700, Patricia Shanahan <(E-Mail Removed)>
wrote:

[snip]

>It is relatively rare to decide, part way through a build, to throw away
>the work so far and begin again with entirely different contents.


I am going to be doing it in a loop. why create a new object
each iteration?

Sincerely,

Gene Wirchenko
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
string vs stringbuilder H.G.Srivatsa ASP .Net 3 08-25-2005 01:55 PM
Double Quotes in a stringbuilder =?Utf-8?B?SnVzdGlu?= ASP .Net 2 03-18-2005 12:57 AM
Does a StringBuilder effect Viewstate variable size? darrel ASP .Net 2 01-28-2005 11:31 PM
How to reset a StringBuilder? ESPN Lover ASP .Net 2 10-14-2004 03:42 PM
no stringbuilder in C#2.0? =?Utf-8?B?SGFuZGk=?= ASP .Net 2 09-11-2004 08:27 AM



Advertisments