Results 1 to 9 of 9
  1. #1
    Jamie is offline Member
    Join Date
    May 2007
    Posts
    60
    Rep Power
    0

    Default How can i insert a char into a string

    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

  2. #2
    levent Guest

    Default

    You can use StringBuffer to do this:

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

  3. #3
    Jamie is offline Member
    Join Date
    May 2007
    Posts
    60
    Rep Power
    0

    Default

    Thanks. It worked :)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default

    Looks like a missing new before the StringBuffer constructor call.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by djl1990 View Post
    okay just a quickie...
    You resurrected a long dead thread; it didn't even smell funny anymore ...

    kind regards,

    Jos

  6. #6
    itstrider is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default

    Quote Originally Posted by djl1990 View Post
    okay just a quickie...

    Java Code:
    
    package dan;
    
    public class addChar {
    
    public static void main(String[] args) {
    
    String str = new String("abde");
    str = StringBuffer(str).insert(2, "C").toString(str);
    
    
        }
    
    }


    'StringBuffer' after 'str =' is showing an error:

    cannot find symbol
    symbol: method StringBuffer(java.lang.String)

    i know this is probably a really stupid question but its annoyed me now
    Use
    Java Code:
    String st = new String("abcde");
    st = [B]new[/B] StringBuffer(st).insert(2, "C").toString();

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by itstrider View Post
    Use
    Java Code:
    String st = new String("abcde");
    st = [B]new[/B] StringBuffer(st).insert(2, "C").toString();
    This zombie thread is long dead, let it r.i.p.

    kind regards,

    Jos

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

    Default

    So... I was trying to use this 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.

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

Similar Threads

  1. SQL Insert Help!!!!
    By shaungoater in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 03:14 AM
  2. Casting an int value into a char
    By kurtulas in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 08:03 PM
  3. Cannot convert from char to String error
    By sondratheloser in forum New To Java
    Replies: 1
    Last Post: 12-13-2007, 09:28 PM
  4. Char to String in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 01:42 PM
  5. Help with, String, Char
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 02:58 PM

Posting Permissions

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