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

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

Description

Returns the number of times sub occurs in str

License

Open Source License

Parameter

Parameter Description
str a parameter
sub a parameter

Declaration

public static int countMatches(String str, String sub) 

Method Source Code

//package com.java2s;
// Released under the Apache License, Version 2.0

public class Main {
    /**/*  ww  w  .j  ava 2 s .co  m*/
     * Returns the number of times sub occurs in str
     * @param str
     * @param sub
     * @return
     */
    public static int countMatches(String str, String sub) {
        int count = 0;
        int initpos = 0;
        while (str.indexOf(sub, initpos) != -1) {
            count = count + 1;
            initpos = str.indexOf(sub, initpos) + 1;
        }
        return count;
    }
}

Related

  1. countMatches(String str, String sub)
  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 string, char find)
  8. countMatches(String string, char toMatch)
  9. countMatches(String string, String other)