Hi All,

I'm fairly new to Java. I have a question involving the Replace Method. Is there, any way I can use the replace method to change a string that results in: Helle, Werld; to get it to result to Hello, World using the replace method. I have seen the Java CharSequence method but not sure if that is a part of the Replace method? Is there a way using the replace method for me to search for the sequence of "le" and replace it with "lo" and alos change "We" to "Wo"? I have to do this using the Replace Method and not sure? This an generic example for a test i'm running. Please see my code below:

public class Printer
{

public static void main(String[] args) 
{
    String test1 = "Holle, Werld!";

    String test1Expected = "Hello, World!";

    String newString1

    newString1 = test1.replace('o', 'e');

    System.out.println("newString1 = " + newString1);//it return back "Helle, Werld!" but I need to return back "Hello, World


}

}

Recommended Answers

All 8 Replies

Your code is doing what you are asking it to do. If you want it to do something different, you need to change the code. What does the replace() method do? What do you expect it to do?

Read the API doc for the replace() method to see how to use it.

I don't think my question is clear. If I use the regular replace method of just the replace(char oldChar, char newChar),
it will change the "Helle" to "Hollo". But I need it to say "Hello". I'm asking is there another functionality within the
replace method that I can use to search for a pattern within a string? For this instance I would like to search for "le" within the string
and replace it with "lo" so that it can say hello. And yes I have checked out the API DOC before coming here and Googled and it wasn't clear to me. So I was looking for some guidance or maybe the correct format to do it?

When you use replace('o', 'e');

You dont replace o and e in the string "holle". You just replace 'o' with an 'e' character.
It doesn't switch the location of the 'o' and 'e' characters.

You would need to make two statements to get this code right.

1. replace('o','e');
2. replace('e','o');

Your code is doing what you are asking it to do, as mentioned above.

Yes I tried that before with two replace methods in usage, it didnt work. In the word "Helle" there are two letter
'e's. I just need to change the last e after the letter l. Using the two replace methods will not work. As it will change
it then to "Hollo".

You are looking for a way to selectively replace letters. I don't think the replace can be told which letters to replace and which ones to leave. I don't think there is any method that can look at a String of letters and selectively replace some and not others. Perhaps a regular expression could be written that would handle your specific case, but it would fail with a different word.

Ah okay thank you NormR1.

If you are wanting to exchange e and o so that every e becomes o and every o becomes e, you must first save one of them, say e by replacing it with one special charcter which does not occur surely in string, like _. Then you change o to e and the special character to o by replacing method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.