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

LGPL

Declaration

public static int countMatches(String str, String sub) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static int countMatches(String str, String sub) {
        if (isEmpty(str) || isEmpty(sub)) {
            return 0;
        }//www.  j  a v  a2 s  . c o  m
        int count = 0;
        int idx = 0;
        while ((idx = str.indexOf(sub, idx)) != -1) {
            count++;
            idx += sub.length();
        }
        return count;
    }

    public static boolean isEmpty(String text) {
        return text == null || text.trim().length() == 0;
    }
}

Related

  1. countMatches(final String str, final String sub)
  2. countMatches(String line, char chr)
  3. countMatches(String reference, String query)
  4. countMatches(String s, String sb)
  5. countMatches(String str, String match)
  6. countMatches(String str, String sub)
  7. countMatches(String str, String sub)
  8. countMatches(String str, String sub)
  9. countMatches(String str, String sub)