Answers for "java string count occurrences of substring"

1

count occurrences of character in string java 8

String someString = "elephant";
long count = someString.chars().filter(ch -> ch == 'e').count();
assertEquals(2, count);
 
long count2 = someString.codePoints().filter(ch -> ch == 'e').count();
assertEquals(2, count2);
Posted by: Guest on June-17-2020
0

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();
}
Posted by: Guest on October-29-2020
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 "java string count occurrences of substring"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language