Here you can find the source of countMatches(String s, String sb)
Parameter | Description |
---|---|
s | the string to check |
sb | the substring to count |
public static int countMatches(String s, String sb)
//package com.java2s; //License from project: Open Source License public class Main { /**/*ww w. j ava 2 s . co m*/ * Retrieve how many times is the substring in the larger string. Null * returns 0. * * @param s * the string to check * @param sb * the substring to count * @return the number of occurances, 0 if the string is null */ public static int countMatches(String s, String sb) { if (s == null || sb == null) { return 0; } int count = 0; int idx = 0; while ((idx = s.indexOf(sb, idx)) != -1) { count++; idx += sb.length(); } return count; } }