replace
is case sensitive only.
There is no "simple" way to do this: you could do two replacements, but that constructs an intermediate string.
String result = s.replace('p', 'b').replace('P', 'B');
I would do this by iterating the character array:
char[] cs = s.toCharArray();
for (int i = 0; i < cs.length; ++i) {
switch (cs[i]) {
case 'p': cs[i] = 'b'; break;
case 'P': cs[i] = 'B'; break;
}
}
String result = new String(cs);
If you wanted to write a method to do this for non-hardcoded letters, you could do it like:
String method(String s, char from, char to) {
char ucFrom = Character.toUpperCase(from); // Careful with locale.
char ucTo = Character.toUpperCase(to);
char[] cs = s.toCharArray();
for (int i = 0; i < cs.length; ++i) {
if (cs[i] == from) { cs[i] = to; }
else if (cs[i] == ucFrom) { cs[i] = ucTo; }
}
return new String(cs);
}