1. Overview
In this article, We'll learn how to count the number of occurrences of the given character is present in the string. This problem is solved in the java programming language using traditional loop, replace and java 8 stream api - filter() and reduce() methods.
All solutions are shown with the example code.
2. Using for loop & Comparison
Here first, we run the for loop on the input string from index 0 to its length and next compare the each character with the given character. If matches then increase the count variable by one.
By end of the string, count value will be no of occurrences of the given character.
This is the simple and easy to understand.
public class CountNoCharExample1 { public static void main(String[] args) { String givenString = "hello world"; char givenChar = 'l'; int strLength = givenString.length(); int count = 0; for (int i = 0; i < strLength; i++) { if(givenChar == givenString.charAt(i)) { count++; } } System.out.println("no of occurrences of char 'l' is "+count); } }
Output:
no of occurrences of char 'l' is 3
3. Using replace() and length()
Another different approach is to replace given character with the empty string "". Next, take the difference between the original string length and replaced string length. Here the differences tells the count for the given character.
package com.javaprogramto.programs.strings.count; public class CountNoCharExample2 { public static void main(String[] args) { String givenString = "hello world"; char givenChar = 'l'; int strOriginalLength = givenString.length(); String replacedString = givenString.replace(String.valueOf(givenChar), ""); int count = strOriginalLength - replacedString.length(); System.out.println("no of occurrences of char 'l' is "+count); } }
Output:
no of occurrences of char 'l' is 3
4. Using Java 8 filter() and count()
Further, we will learn how to get the count of the given character from the string by using java stream api filter() method. This filter() method takes the predicate as condition. If this condition matches then that character is passed to the next stream operation.
after filter() method, we will call count() method to get the no of instances of character.
package com.javaprogramto.programs.strings.count; public class CountNoCharStreamExample3 { public static void main(String[] args) { String givenString = "hello world"; char givenChar = 'l'; long count = givenString.chars().filter(c -> c == givenChar).count(); System.out.println("no of occurrences of char 'l' is " + count); } }
The above code produces the same results as shown in the previous sections.
5. Using Java 8 reduce()
Additionally java 8 is added with the reduce() method which does the reduction operation.
The below example is to get the count for the given char.
package com.javaprogramto.programs.strings.count; public class CountNoCharStreamExample4 { public static void main(String[] args) { String givenString = "hello world"; char givenChar = 'l'; long count = givenString.chars() .filter(c -> c == givenChar) .reduce(0, (a, b) -> a + 1); System.out.println("no of occurrences of char 'l' is " + count); } }
Output:
no of occurrences of char 'l' is 3
6. Using third party apis
This problem can be solved by using third party libraries such as apache commons lang, spring framework and gauva api's.
Methods as follows
Apache: StringUtils.countMatches()
Spring Framework: StringUtils.countOccurrencesOf()
Guava: CharMatcher.is().countIn()
7. Conclusion
In this article, we've seen what are the different ways to get the count of the given character in the string using for loop, java 8 streams and third party apis.
No comments:
Post a Comment
Please do not add any spam links in the comments section.