Results 1 to 3 of 3
  1. #1
    Slayer is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default Replacing characters at a certain index

    I can't seem to find the method that will let me replace a character at a certain index in a string to another character.

    For example, I have a string that is just ten a's, "aaaaaaaaaa"
    And at the 6th spot I want to replace the a with a b. How would I do that?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by Slayer View Post
    I can't seem to find the method that will let me replace a character at a certain index in a string to another character.

    For example, I have a string that is just ten a's, "aaaaaaaaaa"
    And at the 6th spot I want to replace the a with a b. How would I do that?
    Copy your original String to a StringBuilder (or StringBuffer) and do the replacements in that object. When finished have that object create a new String. Or, you can make your String create a char array, do your work in there and create a new String given the modified char array. Read the details of this all in the API documentation for those classes.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,801
    Rep Power
    8

Similar Threads

  1. Searching and replacing characters in a File
    By DBaskov in forum New To Java
    Replies: 3
    Last Post: 02-01-2011, 12:39 AM
  2. Replies: 0
    Last Post: 10-29-2010, 08:15 PM
  3. How to index the special characters in Lucene
    By talktoudaykumar in forum Lucene
    Replies: 2
    Last Post: 04-23-2009, 07:51 AM
  4. replacing characters???
    By manda147 in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 09:19 AM
  5. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 07:33 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
  •