【问题标题】:Finding second occurrence of a substring in a string in Java在Java中的字符串中查找第二次出现的子字符串
【发布时间】:2013-10-02 20:57:42
【问题描述】:

我们得到一个字符串,比如"itiswhatitis" 和一个子字符串,比如"is"。 当字符串"is" 在原始字符串中第二次出现时,我需要找到'i' 的索引。

String.indexOf("is") 在这种情况下将返回 2。在这种情况下,我希望输出为 10。

【问题讨论】:

    标签: java string substring indexof


    【解决方案1】:

    这似乎是一个很好的聚会......我参加了:

    public static int nthIndexOf(String str, String subStr, int count) {
        int ind = -1;
        while(count > 0) {
            ind = str.indexOf(subStr, ind + 1);
            if(ind == -1) return -1;
            count--;
        }
        return ind;
    }
    

    【讨论】:

      【解决方案2】:

      我希望我不会迟到。这是我的答案。我喜欢使用 Pattern/Matcher,因为它使用了应该更有效的正则表达式。然而,我认为这个答案可以得到加强:

          Matcher matcher = Pattern.compile("is").matcher("I think there is a smarter solution, isn't there?");
          int numOfOcurrences = 2;
          for(int i = 0; i < numOfOcurrences; i++) matcher.find();
          System.out.println("Index: " + matcher.start());
      

      【讨论】:

      • 不错的解决方案,我找到了。谢谢哈斯娜!
      【解决方案3】:

      如果您想查找超过 2 次的索引:

      public static int ordinalIndexOf(String fullText,String subText,int pos){
      
          if(fullText.contains(subText)){
              if(pos <= 1){
                  return fullText.indexOf(subText);
              }else{
                  --pos;
                  return fullText.indexOf(subText, ( ordinalIndexOf(fullText,subText,pos) + 1) );
              }
          }else{
              return -1;
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        使用indexOf()的重载版本,它将起始索引(fromIndex)作为第二个参数:

        str.indexOf("is", str.indexOf("is") + 1);
        

        【讨论】:

        • 这很巧妙。
        【解决方案5】:

        我正在使用: Apache Commons Lang: StringUtils.ordinalIndexOf()

        StringUtils.ordinalIndexOf("Java Language", "a", 2)
        

        【讨论】:

          【解决方案6】:

          你可以写一个函数来返回出现位置的数组,Java有String.regionMatches函数,非常方便

          public static ArrayList<Integer> occurrencesPos(String str, String substr) {
              final boolean ignoreCase = true;
              int substrLength = substr.length();
              int strLength = str.length();
          
              ArrayList<Integer> occurrenceArr = new ArrayList<Integer>();
          
              for(int i = 0; i < strLength - substrLength + 1; i++) {
                  if(str.regionMatches(ignoreCase, i, substr, 0, substrLength))  {
                      occurrenceArr.add(i);
                  }
              }
              return occurrenceArr;
          }
          

          【讨论】:

            【解决方案7】:

            我认为可以使用循环。

            1 - check if the last index of substring is not the end of the main string.
            2 - take a new substring from the last index of the substring to the last index of the main string and check if it contains the search string
            3 - repeat the steps in a loop
            

            【讨论】:

              【解决方案8】:
              int first = string.indexOf("is");
              int second = string.indexOf("is", first + 1);
              

              此重载开始从给定索引中查找子字符串。

              【讨论】:

              • 出现两次以上怎么办?
              • 那就没什么特别的了,还是要第二次发生。
              • 第三次出现的索引呢!
              • 怎么样?问题是找到第二次出现。
              • @PravatPanda:我猜你想知道如何获得第三次出现?然后您可以继续 Jeroen 的答案中的代码并添加第三个 string.indexOf("is", second + 1); 尽管创建一个返回第 N 个 indexOf 的方法可能会更好
              猜你喜欢
              • 2010-12-25
              • 2020-02-26
              • 2018-08-24
              • 2011-08-06
              • 1970-01-01
              • 2018-03-22
              • 2011-02-04
              • 2012-07-11
              相关资源
              最近更新 更多