Answers for "how to count substring in a string in java"

0

how to count an replace substring string in java

String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count = 0;
while(lastIndex != -1){
    lastIndex = str.indexOf(findStr,lastIndex);
    if(lastIndex != -1){
        count ++;
        lastIndex += findStr.length();
    }
}
System.out.println(count);
Posted by: Guest on June-07-2020

Code answers related to "how to count substring in a string in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language