I want to replace all 'A' of a string by 'B', and all 'B' by 'Z' so that the result of "ABCAAB" will be "BZCBBZ".
Is there a way to replace the following code using the replaceAll function?
String init = "ABCAAB"
String res = "";
for (char c: init.toCharArray()){
switch (c) {
case 'A':res = res+'B';
case 'B':res = res+'Z';
default :res = res+c;
}
}
String res = init.replaceAll("B", "Z").replaceAll("A", "B");String res = init.replace("B", "Z").replace("A", "B");