Here you can find the source of countSubstringOccurrances(String s1, String SubString)
Parameter | Description |
---|---|
s1 | non-null input string to test |
SubString | non-null Substring to cound |
public static int countSubstringOccurrances(String s1, String SubString)
//package com.java2s; //License from project: Apache License public class Main { /**/* ww w .j a va 2 s. c o m*/ * return the number of times the substring occurs in the text - * where there are overlaps the count tells how many times the substring could be replaced * @param s1 non-null input string to test * @param SubString non-null Substring to cound * @return non-negative number of occurrances of the substring */ public static int countSubstringOccurrances(String s1, String SubString) { int count = 0; int index = s1.indexOf(SubString); int sublength = SubString.length(); int start = 0; while (index > -1) { count++; start = index + sublength; if (start > s1.length() - sublength) return (count); index = s1.indexOf(SubString, start); } return (count); } }