I need to swap letters in a string with the following rules:
Searching for java "A to T, T to A" found this suggestion:
String sequence = "AATTTCTCGGTTTCAAT";
sequence = sequence.replace("A", "t")
.replace("T", "a")
.replace("C", "g")
.replace("G", "c")
.toUpperCase();
System.out.println(sequence);
This is a simple and concise solution that works for your specific situation and will have acceptable performance if your DNA strings are relatively short. For a more general solution for handling large amounts of data you should iterate over the characters one by one and build a new string. Or as polygenelubricants pointed out - consider a storage format that only uses 2 bits per base instead of 16.