【问题标题】:Finding multiple hits with indexOf使用 indexOf 查找多个匹配项
【发布时间】:2014-01-15 20:34:17
【问题描述】:

我想用 indexOf 方法查找多个匹配项。

我有这行要搜索:

public static final String DNA = "ATGTTGCTGGCATAGATGTTAACTTCCAC";

例如,我想在这个字符串中搜索 AC 的位置。我想用这种方法做到这一点。目前我只得到 21 次。因为我应该得到 21 和 27 个。

public int[] zoek(String gezocht)
{
    // goed = 21+27 bij AC
    int fromIndex = 0;
    for (int i = 0; i < DNA.length(); i++)
    {
       fromIndex = DNA.indexOf(gezocht, fromIndex);
       dna.add(fromIndex);

    }
    System.out.println(dna);
    return zoek;
}

【问题讨论】:

  • 为什么那个字符串是最终的。字符串已经成为不可变的。
  • @SaurabhSharma 这使它成为一个常量(您不能将变量中的引用替换为对不同不可变 String 的引用)。
  • 使用正则表达式:stackoverflow.com/questions/8938498/…
  • @chrylis 哦,是的!谢谢。 :)

标签: java indexof


【解决方案1】:

试试这个代码。

import java.util.ArrayList;

public class Test001
{
    public static final String DNA = "ATGTTGCTGGCATAGATGTTAACTTCCAC";

    public static Integer[] zoek(String gezocht)
    {
        ArrayList<Integer> dna = new ArrayList<Integer>();
        int i = 0;
        while (i < DNA.length())
        {
           int fromIndex = DNA.indexOf(gezocht, i);
           if (fromIndex >= 0){
               dna.add(fromIndex);
               i = fromIndex + 1;
           }else{
               break;
           }
        }
        System.out.println(dna);
        Integer[] zoek = new Integer[dna.size()]; 
        return dna.toArray(zoek);
    }

    public static void main(String args[]) {
        Integer[] ind = zoek("AC");
        for (int fromIndex : ind){
            System.out.println(fromIndex);
        }
    }
}

顺便说一句,我会以不同的方式命名变量。

import java.util.ArrayList;

public class Test001
{
    public static final String DNA = "ATGTTGCTGGCATAGATGTTAACTTCCAC";

    public static Integer[] search(String sought)
    {
        ArrayList<Integer> hits = new ArrayList<Integer>();
        int startIndex = 0;
        while (startIndex < DNA.length())
        {
           int foundIndex = DNA.indexOf(sought, startIndex);
           if (foundIndex >= 0){
               hits.add(foundIndex);
               startIndex = foundIndex + 1;
           }else{
               break;
           }
        }
        System.out.println(hits);
        Integer[] found = new Integer[hits.size()]; 
        return hits.toArray(found);
    }

    public static void main(String args[]) {
        Integer[] found = search("AC");
        for (int foundIndex : found){
            System.out.println(foundIndex);
        }
    }
}

【讨论】:

    【解决方案2】:

    改用 while 循环,例如:

    int fromIndex = 0;
    while((fromIndex = dna.indexOf(gezocht, fromIndex)) != -1) {
        ...
        fromIndex++; // We don't want to find the previous match again
    }
    

    【讨论】:

      【解决方案3】:

      您必须向fromIndex 添加一个,这样indexOf 就不会再次找到相同的匹配项。你也不应该循环尽可能多的字符串有字符,但只要找到出现:

      int fromIndex = -1;
      while ((fromIndex = DNA.indexOf(gezocht, fromIndex + 1)) != -1) {
         dna.add(fromIndex);
      }
      

      【讨论】:

        猜你喜欢
        • 2012-09-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-17
        • 1970-01-01
        • 2015-06-01
        • 2016-12-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多