Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: What is the easiest way to change a single character in a string?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default What is the easiest way to change a single character in a string?

    Hi, I have a simple question regarding Strings. I'm working on a short program that will require me to remove a single character from a string and replace it with another character, if that makes sense.

    So basically If I had the string: "dogs"
    I want to be able to replace the second character with a 'u' to look like this: "dugs"
    What is the best way to do that?

    I took a look through the String Methods as listed on oracle.com and I didn't find anything that does this. I did find the "replace(char oldChar, char newChar) " method, but from the looks of it, it replaces all instances of a given character. What I want is something that will replace only the one character specified. I'm sure that this could be done fairly painlessly via concatenation, But I thought I would ask first if there was a better way.

    Any help is appreciated
    Last edited by Jumbosize; April 26th, 2012 at 04:03 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is the easiest way to change a single character in a string?

    dogs = dogs.replace('o','u');

    This take a specified character and replaces it.

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: What is the easiest way to change a single character in a string?

    The best way is to use a StringBuilder. With stringbuilder, you could do this

    StringBuilder stBuilder = new StringBuilder("dogs");
     
    stBuilder.setCharAt(1, 'u');
    Last edited by Tjstretch; April 26th, 2012 at 04:21 PM.

Similar Threads

  1. Single Character Validation
    By dannybrgc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 21st, 2012, 06:20 PM
  2. Help using String and character analyzers such as isDigit() and isLetter()
    By scholaryoshi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 6th, 2012, 05:24 PM
  3. How to change a String value into a number and then back into a String.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 18th, 2011, 12:43 PM
  4. How to convert or break String into Character
    By Bharath12 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 14th, 2011, 11:41 PM
  5. Conversion from multiple ints to single string
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 09:08 PM