Skip to content

Commit 609ba4a

Browse files
TSobalazhendrikse
authored andcommitted
[BAEL-829] Number of occurrences of a char in a String (eugenp#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 66471cf. * Revert of evaluation task "injecting beans" This reverts commit d2ac201. * [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.
1 parent 5cfb7b0 commit 609ba4a

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.baeldung.java.countingChars;
2+
3+
import com.google.common.base.CharMatcher;
4+
import org.apache.commons.lang.StringUtils;
5+
import org.junit.Test;
6+
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
13+
/***
14+
* Example of counting chars in a String.
15+
*/
16+
public class CountCharsExample {
17+
18+
@Test
19+
public void givenString_whenUsingLoop_thenCountChars() {
20+
String someString = "elephant";
21+
char someChar = 'e';
22+
int count = 0;
23+
for (int i = 0; i < someString.length(); i++) {
24+
if (someString.charAt(i) == someChar) {
25+
count++;
26+
}
27+
}
28+
assertEquals(2, count);
29+
}
30+
31+
@Test
32+
public void givenString_whenUsingReplace_thenCountChars() {
33+
String someString = "elephant";
34+
int count = someString.length() - someString.replace("e", "").length();
35+
assertEquals(2, count);
36+
}
37+
38+
@Test
39+
public void givenString_whenUsingSplit_thenCountChars() {
40+
String someString = "elephant";
41+
int count = someString.split("e", -1).length - 1;
42+
assertEquals(2, count);
43+
}
44+
45+
@Test
46+
public void givenString_whenUsingReqExp_thenCountChars() {
47+
Pattern pattern = Pattern.compile("[^e]*e");
48+
Matcher matcher = pattern.matcher("elephant");
49+
int count = 0;
50+
while (matcher.find()) {
51+
count++;
52+
}
53+
assertEquals(2, count);
54+
}
55+
56+
@Test
57+
public void givenString_whenUsingRecursion_thenCountChars() {
58+
int count = useRecursion("elephant", 'e', 0);
59+
assertEquals(2, count);
60+
}
61+
62+
private int useRecursion(String someString, char searchedChar, int index) {
63+
if (index >= someString.length()) {
64+
return 0;
65+
}
66+
67+
int count = someString.charAt(index) == searchedChar ? 1 : 0;
68+
return count + useRecursion(someString, searchedChar, index + 1);
69+
}
70+
71+
@Test
72+
public void givenString_whenUsingStringUtils_thenCountChars() throws InterruptedException {
73+
int count = StringUtils.countMatches("elephant", "e");
74+
assertEquals(2, count);
75+
}
76+
77+
@Test
78+
public void givenString_whenUsingJava8Features_thenCountChars() {
79+
String someString = "elephant";
80+
long count = someString.chars().filter(ch -> ch == 'e').count();
81+
assertEquals(2, count);
82+
83+
long count2 = someString.codePoints().filter(ch -> ch == 'e').count();
84+
assertEquals(2, count2);
85+
}
86+
87+
@Test
88+
public void givenString_whenUsingGuavaCharMatcher_thenCountChars() {
89+
int count = CharMatcher.is('e').countIn("elephant");
90+
assertEquals(2, count);
91+
}
92+
93+
}

0 commit comments

Comments
 (0)