Here you can find the source of countMatches(String str, String sub)
Parameter | Description |
---|---|
str | a parameter |
sub | a parameter |
public static int countMatches(String str, String sub)
//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; } }