java find all instances of substring in string code example
Example 1: find all possible substrings of a string java
for (int i = 0; i < A.length(); i++) {
for (int j = i+1; j <= A.length(); j++) {
System.out.println(A.substring(i,j));
}
}
Example 2: find number of occurrences of a substring in a string java
public static int count(String str, String target) {
return (str.length() - str.replace(target, "").length()) / target.length();
}