I need to swap letters in a string with the following rules:
I would go for a more general solution like this:
public String tr(String original, String trFrom, String trTo) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < original.length(); ++i) {
int charIndex = trFrom.indexOf(original.charAt(i));
if (charIndex >= 0) {
sb.append(trTo.charAt(charIndex));
} else {
sb.append(original.charAt(i));
}
}
return sb.toString();
}
Calling the function like this would give the result you need:
tr("ACGTA", "ATCG", "TAGC")
So the function is pretty much the same as unix tr utility:
echo ACGTA | tr ATCG TAGC