Results 1 to 6 of 6
  1. #1
    Hallowed is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default How to use a StringBuffer?

    So... I was trying to use this:


    Is there any method available to insert a character at a specific position in a string?
    For example:

    st = "abde" <------ Original string
    st= "abCde" <------ Add "C" in position 2

    You can use StringBuffer to do this:

    String st = new String("abcde");
    st = StringBuffer(st).insert(2, "C").toString();



    as an example for what I'm trying to do. Basically I just want to add a 1 or a 0 to the end of a pre-existing String. <-binary conversion stuff.
    Anyway, so when i try to do it, Eclipse keeps telling me a I need to create a method for StringBuffer(String)..

    example..
    output = StringBuffer(output).insert("0").toString();

    But it should be noted that I've already included: import java.lang.*;
    in my code... Help?
    Sorry if this seems dumb. I'm extremely new to java and coding in general.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The example I think forgot the word "new" in front of StringBuffer since you need to create a new StringBuffer object for this to work. Myself, I'd probably just use simple String concatenation unless you're doing this multiple times inside of a large or critical loop. If so, even then I'd not use a StringBuffer since it has extra unnecessary (in this case) overhead for thread-safety, and would use a StringBuilder object instead. You would use append(...) if adding to the end of the character sequence.

    So for example

    Java Code:
    output += "0";  // simple way
    
    output = [color="red"][b]new[/b][/color] StringBuilder(output).append("0").toString(); // if in a critical loop or a possible bottleneck
    Last edited by Fubarable; 02-17-2011 at 09:11 PM.

  3. #3
    Hallowed is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    oh that was it. Thanks so much. =)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're quite welcome.

    Correction: if in a critical loop though, I'd create the StringBuilder object before the loop, append junk in inside the loop, and only call toString() on the StringBuilder after the loop.

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by Hallowed View Post
    But it should be noted that I've already included: import java.lang.*;
    Hallowed,

    You don't really need to import this package. This package comes inbuilt for every java code. :p

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    Hallowed is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    Oh alright haha. I just read on a website to use it with that code. So I dunno =/

Similar Threads

  1. How to do this with StringBuffer???
    By mlibot in forum New To Java
    Replies: 2
    Last Post: 03-24-2010, 12:40 AM
  2. Replies: 3
    Last Post: 03-04-2009, 06:03 AM
  3. Replies: 1
    Last Post: 03-03-2009, 05:27 AM
  4. StringBuffer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 08:33 AM
  5. Help with StringBuffer
    By Marcus in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 05:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •