[Solved] I’m trying to create a program which changes all the letters from a string in arrays


There is a method for Strings, called replaceAll, which you can use to replace parts of the String, like:

  "ATGCATGC GTCGTGA .".replaceAll ("A", "T");

In the upcomming java9, there is a jshell, ideally suited to test such things.

-> "ATGCATGC GTCGTGA .".replaceAll ("A", "T")
|  Expression value is: "TTGCTTGC GTCGTGT ."
|    assigned to temporary variable $85 of type String

So on the first look, it seems, as if calling 4 such methods in a chain will give the result, but:

-> "ATGCATGC GTCGTGA .".replaceAll ("A", "T").replaceAll ("T", "A").replaceAll ("G", "C").replaceAll ("C", "G")
|  Expression value is: "AAGGAAGG GAGGAGA ."
|    assigned to temporary variable $1 of type String

the transformed As are retransformed back from the next transformation, but we can use a trick – transform to lowercase:

-> "ATGCATGC GTCGTGA .".replaceAll ("A", "t").replaceAll ("T", "a").replaceAll ("G", "c").replaceAll ("C", "g")
|  Expression value is: "tacgtacg cagcact ."
|    assigned to temporary variable $2 of type String

and in the end, make a call to uppercase everything:

-> String dna = "ATGCATGC GTCGTGA .".replaceAll ("A", "t").replaceAll ("T", "a").replaceAll ("G", "c").replaceAll ("C", "g").toUpperCase ()
|  Modified variable dna of type String with initial value "TACGTACG CAGCACT ."

1

solved I’m trying to create a program which changes all the letters from a string in arrays