Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Gene Wirchenko Newsgroups: comp.lang.java.programmer Subject: Re: StringBuilder Difficulties Date: Wed, 29 Jun 2011 15:00:39 -0700 Organization: A noiseless patient Spider Lines: 40 Message-ID: <9v7n071ns1n0plisods3m2pi6q68rtket5@4ax.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Injection-Info: mx04.eternal-september.org; posting-host="7Qrvczazr82YckO5XW8Vtw"; logging-data="17732"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18tQMFgS9TZ43Mkhi3ac/jZo5pbCj77HRg=" X-Newsreader: Forte Agent 4.2/32.1118 Cancel-Lock: sha1:ywxjZSQTfy5tCT1nZtHgSJM8AWg= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5785 On Wed, 29 Jun 2011 14:38:04 -0700, Patricia Shanahan 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