finding which sub string comes first, which comes

2019-09-22 00:44发布

问题:

String s="CCATGTTGGCCTAGGTGACAC";

I am trying to find an Open reading frame from above DNA sequence. First I have to find whether there is "ATG" present as a sub string. If it is there then I have to find out whether any one of these sub strings is present "TAA", "TAG", "TGA". Among these three sub strings whichever comes first is to be taken into considerartion and then I would print the string from "ATG" to "TAA" or "TAG" or "TGA" whichever comes first. From above string output should be "ATG TTGGCC TAG".

回答1:

You can get the index of each substring..

System.out.println(s.indexOf("ax"));

The one whose index is minimum is the one who comes first.



回答2:

    String testString = "ddddeeaxgghnnnbykkkkkklllllczfr";


    String[] frags = testString.split("(ax|by|cz)");
    if(frags.length >= 1) {
        System.out.println("first=" + frags[0]);
        if(frags.length >= 2) {
            System.out.println("second=" + frags[1]);
            if(frags.length >= 3) {
                System.out.println("third=" + frags[2]);
            }
        }
    } else {
        System.out.println("nothing found");
    }

It prints:

first=ddddee
second=gghnnn
third=kkkkkklllll