Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何搜索和汇总字符串;CTG“;在另一个字符串中,哪个是文件?_Java_String - Fatal编程技术网

Java 如何搜索和汇总字符串;CTG“;在另一个字符串中,哪个是文件?

Java 如何搜索和汇总字符串;CTG“;在另一个字符串中,哪个是文件?,java,string,Java,String,如何从读取为字符串的文件中搜索字符串CTG?然后数一数它出现了多少次? 例如,我将如何在此处或任何地方添加代码来执行此操作: public String readStrFromFile(){ FileResource readFile = new FileResource(); String DNA = readFile.asString(); //System.out.println("DNA: " + DNA); return DNA; }//end

如何从读取为字符串的文件中搜索字符串CTG?然后数一数它出现了多少次? 例如,我将如何在此处或任何地方添加代码来执行此操作:

public String readStrFromFile(){

    FileResource readFile = new FileResource();

    String DNA = readFile.asString();

    //System.out.println("DNA: " + DNA);

    return DNA;

}//end readStrFromFile() method;

可以使用正则表达式

String DNA = readFile.asString();
Pattern pattern = Pattern.compile("CTG");
Matcher matcher = pattern.matcher(DNA);
while(matcher.find()){
    System.out.println(matcher.group() + " at " + matcher.start());
}
或者,如果文件太大,则应使用KMP算法或类似算法

编辑:
你可以有一个柜台

int count = 0; 
while(matcher.find()){
    count++;
    System.out.println(matcher.group() + " at " + matcher.start());
}
System.out.println("Number of count : " + count);

我该如何计算所有这些CTG?我需要知道你们能用这个计数器做多少。