Java String Match Count countMatches(String str, String sub)

Here you can find the source of countMatches(String str, String sub)

Description

count Matches

License

Open Source License

Declaration

private static int countMatches(String str, String sub) 

Method Source Code

//package com.java2s;

public class Main {
    private static int countMatches(String str, String sub) {
        if (str == null || str.length() == 0 || sub == null || sub.length() == 0) {
            return 0;
        }//ww w.j av a 2  s  .  c om
        int count = 0;
        int idx = 0;
        while ((idx = str.indexOf(sub, idx)) != -1) {
            count++;
            idx += sub.length();
        }
        return count;
    }
}

Related

  1. countMatches(String str, String match)
  2. countMatches(String str, String sub)
  3. countMatches(String str, String sub)
  4. countMatches(String str, String sub)
  5. countMatches(String str, String sub)
  6. countMatches(String str, String sub)
  7. countMatches(String str, String sub)
  8. countMatches(String string, char find)
  9. countMatches(String string, char toMatch)