I want to modify two characters in the string, for example change each 'i'
into 'e'
, and each 'e'
into 'i'
so text like "This is a test"
will become "Thes es a tist"
.
I've made a solution that works, but it is boring and inelegant:
String input = "This is a test";
char a = 'i';
char b = 'e';
char[] chars = input.toCharArray();
for(int i = 0; i < chars.length; i++) {
if(chars[i] == a) {
chars[i] = b;
}else if(chars[i] == b) {
chars[i] = a;
}
}
input = new String(chars);
How can this be accomplished with regex?