Here you can find the source of countMatches(String string, String other)
string
contains other
.
Parameter | Description |
---|---|
string | the string to search |
other | the string that is searched |
public static int countMatches(String string, String other)
//package com.java2s; //License from project: Apache License public class Main { /**/* w w w . j av a 2 s . c o m*/ * Returns how many times <code>string</code> contains * <code>other</code>. * @param string the string to search * @param other the string that is searched * @return the number of occurences */ public static int countMatches(String string, String other) { if (null == string) return 0; if (null == other) return 0; if (0 >= string.length()) return 0; if (0 >= other.length()) return 0; int count = 0; int index = 0; while ((index <= string.length() - other.length()) && (-1 != (index = string.indexOf(other, index)))) { count++; index += other.length(); } return count; } }