How to change a char in a String - replacing letters java

Viewed 8581

Simular topics were not able to solve my problem. I need to change the char 'a' to 'x' in an given String str.

Example: "abc" = "xbc". I am only allowed to use substring(), charAt() - no replace() method.

My code so far:

public static String ersetze(String text){
    for(int i = 0; i<text.length(); i++){

        if(text.substring(i, i+1).charAt(i) == 'a'){
            text.substring(i, i+1) = 'x'; 
        }
    }
    //return statement
}

Now the error is text.substring(i, i+1) = 'x';that the left assignment must be a variable - clear. But how to assigne the letter to a variable now? If I declare a char x; how to put that x in the String to replace the letter?

4 Answers
Related