How about using StringUtils.countMatches from Apache Commons Lang?
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
System.out.println(StringUtils.countMatches(str, findStr));
That outputs:
3
More Related Contents:
- java , inserting between duplicate chars in String [closed]
- Write a function that will sort a string array using Java
- String class replace() method [closed]
- Reading String for list of Integers separated by spaces [duplicate]
- How do I compare strings in Java?
- String.equals versus == [duplicate]
- How do I count the number of occurrences of a char in a String?
- How to unescape a Java string literal in Java?
- Converting A String To Hexadecimal In Java
- Java String replace not working [duplicate]
- What’s the best way to build a string of delimited items in Java?
- How to sort an array of objects in Java?
- How to split a string between letters and digits (or between digits and letters)?
- How can non-ASCII characters be removed from a string?
- How to capitalize the first letter of a String in Java?
- Fastest way to iterate over all the chars in a String
- Using Enum values as String literals
- Java String.indexOf and empty Strings
- Split a string, at every nth position
- Add spaces between the characters of a string in Java?
- Java: removing numeric values from string
- String concatenation with Null
- Problems with newline in Graphics2D.drawString
- Validating IPv4 string in Java
- Use string in switch case in java
- Difference between String trim() and strip() methods in Java 11
- Comparing a string with the empty string (Java)
- Java Scanner class reading strings
- Create a SortedMap in Java with a custom Comparator
- Java replace all square brackets in a string