1

I have a string

String seq = "AGAGTTAAGTG+A";

I want to remove all the characters except A,C,G and T. So that string can change into

String newseq ="AGAGTTAAGTGA"

I have tried below code but that didn't really work:

String result = str.replaceAll("[^!+]", "");

I heard StringBuilder method can be used, but I am not sure how I'd use it. Can anyone advise me on how to do this?

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
  • 2
    What have you tried so far? – Turing85 Sep 27 '20 at 16:12
  • 1
    Stack Overflow users are not here to do your job for you. We are here to help you solve any problems you might have in your attempt at solving the problem. So please show us what you have tried and tell us what didn't work as expected. – JustAnotherDeveloper Sep 27 '20 at 16:13
  • im stuck on the beginning where I have to make a string only consist of A,C,G,T. I did not meant to ask others to do my job. I apologize. – 몰라나도 Sep 27 '20 at 16:18
  • The set of valid characters is smaller than the set of invalid characters. It's far easier to iterate through the string and build a new string with only the valid characters than to remove the invalid characters from the original string. – JustAnotherDeveloper Sep 27 '20 at 16:23
  • should make a string into char and return as string agian? – 몰라나도 Sep 27 '20 at 16:25
  • 1
    *Some people says we can use StringBuilder method,* - I would agree. *but i am not sure about how to use it.* - did you search the forum for examples that use the StringBuilder class? That is one way to learn. In any case, tou just iterate through each character in the String. If it is "A, C, G, T", you add the character to the StringBuilder. Its like 3 lines of code. One for the for loop, one for the if statement and another to add the character to the StringBuilder. Then when the loop is finished you invoke the toString() method on the StringBuilder to get your new String. – camickr Sep 27 '20 at 16:36
  • Your problems is very basic, and has many solutions. 1) copy chars from seq into a new seq only if it's one of ATGC 2) replace anything that is not ATGC by '' (no character) etc. You want to think about the idea and solve it first instead trying to do what others tell you to do; using `StringBuilder` seems to be too complicated for you at the moment, so maybe go with what you know/understand, and char/string basic manipulation, and then use functions with hidden algorithm (StringBuilder) – Soleil Sep 27 '20 at 16:56
  • Thank you I got it !! – 몰라나도 Sep 27 '20 at 17:11
  • *Thank you I got it !!* - post your solution for the benefit of others. – camickr Sep 27 '20 at 17:19

1 Answers1

0

You can use the ^ character to negate a group:

String result = seq.replaceAll("[^ACGT]", "");
Mureinik
  • 297,002
  • 52
  • 306
  • 350