From 609ba4ab1da78b20ff0d78dcc7e135b2bbe4db07 Mon Sep 17 00:00:00 2001 From: Tomasz Sobala Date: Fri, 12 May 2017 16:43:28 +0200 Subject: [PATCH] [BAEL-829] Number of occurrences of a char in a String (#1785) * injecting beans * XML-based configuration replaced with Java Config. * [BAEL-431] Exploring TestRestTemplate. * Revert of evaluation task "XML-based configuration replaced with Java Config." This reverts commit 66471cf0574c85f8ff514ec4caf5ba44ebba1a74. * Revert of evaluation task "injecting beans" This reverts commit d2ac20185e636245bc0ae0b4ccb952965de88e28. * [BAEL-431] fix to the tests in TestRestTemplateBasicLiveTest. * [BAEL-431] added more meaningful user and password for auth. * [BAEL-820] examples of wait() and sleep() methods. * [BAEL-820] wait() and sleep() examples. * [BAEL-829] number of occurences of a char in a String. * [BAEL-829] printlns changed to assertions. * [BAEL-829] improved names of the tests. --- .../java/countingChars/CountCharsExample.java | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/java/countingChars/CountCharsExample.java diff --git a/core-java/src/test/java/com/baeldung/java/countingChars/CountCharsExample.java b/core-java/src/test/java/com/baeldung/java/countingChars/CountCharsExample.java new file mode 100644 index 0000000000..83c3be1683 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/countingChars/CountCharsExample.java @@ -0,0 +1,93 @@ +package com.baeldung.java.countingChars; + +import com.google.common.base.CharMatcher; +import org.apache.commons.lang.StringUtils; +import org.junit.Test; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.junit.Assert.assertEquals; + + +/*** + * Example of counting chars in a String. + */ +public class CountCharsExample { + + @Test + public void givenString_whenUsingLoop_thenCountChars() { + String someString = "elephant"; + char someChar = 'e'; + int count = 0; + for (int i = 0; i < someString.length(); i++) { + if (someString.charAt(i) == someChar) { + count++; + } + } + assertEquals(2, count); + } + + @Test + public void givenString_whenUsingReplace_thenCountChars() { + String someString = "elephant"; + int count = someString.length() - someString.replace("e", "").length(); + assertEquals(2, count); + } + + @Test + public void givenString_whenUsingSplit_thenCountChars() { + String someString = "elephant"; + int count = someString.split("e", -1).length - 1; + assertEquals(2, count); + } + + @Test + public void givenString_whenUsingReqExp_thenCountChars() { + Pattern pattern = Pattern.compile("[^e]*e"); + Matcher matcher = pattern.matcher("elephant"); + int count = 0; + while (matcher.find()) { + count++; + } + assertEquals(2, count); + } + + @Test + public void givenString_whenUsingRecursion_thenCountChars() { + int count = useRecursion("elephant", 'e', 0); + assertEquals(2, count); + } + + private int useRecursion(String someString, char searchedChar, int index) { + if (index >= someString.length()) { + return 0; + } + + int count = someString.charAt(index) == searchedChar ? 1 : 0; + return count + useRecursion(someString, searchedChar, index + 1); + } + + @Test + public void givenString_whenUsingStringUtils_thenCountChars() throws InterruptedException { + int count = StringUtils.countMatches("elephant", "e"); + assertEquals(2, count); + } + + @Test + public void givenString_whenUsingJava8Features_thenCountChars() { + 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); + } + + @Test + public void givenString_whenUsingGuavaCharMatcher_thenCountChars() { + int count = CharMatcher.is('e').countIn("elephant"); + assertEquals(2, count); + } + +}