Skip to content

Commit 2253454

Browse files
authored
[count-substr] count seq in a string (#18814)
* [count-substr] count seq in a string * [count-substr] move to -6 module
1 parent 02e2672 commit 2253454

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>core-java-string-algorithms-6</artifactId>
6+
<packaging>jar</packaging>
7+
<name>core-java-string-algorithms-6</name>
8+
9+
<parent>
10+
<groupId>com.baeldung.core-java-modules</groupId>
11+
<artifactId>core-java-modules</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.apache.commons</groupId>
18+
<artifactId>commons-lang3</artifactId>
19+
<version>${common-lang3.version}</version>
20+
</dependency>
21+
</dependencies>
22+
23+
<properties>
24+
<common-lang3.version>3.18.0</common-lang3.version>
25+
</properties>
26+
27+
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.countseq;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
import org.apache.commons.lang3.StringUtils;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class CountSequenceInStringUnitTest {
12+
13+
private final static String INPUT = "This is a test string. This test is for testing the count of a sequence in a string. This string has three sentences.";
14+
15+
int countSeqByIndexOf(String input, String seq) {
16+
int count = 0;
17+
int index = input.indexOf(seq);
18+
while (index != -1) {
19+
count++;
20+
index = input.indexOf(seq, index + seq.length());
21+
}
22+
return count;
23+
}
24+
25+
@Test
26+
void whenUsingIndexOf_thenCorrect() {
27+
assertEquals(3, countSeqByIndexOf(INPUT, "string"));
28+
assertEquals(2, countSeqByIndexOf(INPUT, "string."));
29+
}
30+
31+
int countSeqByRegexFind(String input, String seq) {
32+
// Alternative: Pattern pattern = Pattern.compile(seq, Pattern.LITERAL);
33+
Matcher matcher = Pattern.compile(Pattern.quote(seq))
34+
.matcher(input);
35+
int count = 0;
36+
while (matcher.find()) {
37+
count++;
38+
}
39+
return count;
40+
}
41+
42+
@Test
43+
void whenUsingRegexFind_thenCorrect() {
44+
assertEquals(3, countSeqByRegexFind(INPUT, "string"));
45+
assertEquals(2, countSeqByRegexFind(INPUT, "string."));
46+
}
47+
48+
int countSeqByRegexSplit(String input, String seq) {
49+
Pattern pattern = Pattern.compile(seq, Pattern.LITERAL);
50+
return pattern.split(input, -1).length - 1;
51+
}
52+
53+
@Test
54+
void whenUsingRegexSplit_thenCorrect() {
55+
assertEquals(3, countSeqByRegexSplit(INPUT, "string"));
56+
assertEquals(2, countSeqByRegexSplit(INPUT, "string."));
57+
}
58+
59+
int countSeqByStream(String input, String seq) {
60+
long count = Pattern.compile(Pattern.quote(seq))
61+
.matcher(input)
62+
.results()
63+
.count();
64+
return Math.toIntExact(count);
65+
}
66+
67+
@Test
68+
void whenUsingStream_thenCorrect() {
69+
assertEquals(3, countSeqByStream(INPUT, "string"));
70+
assertEquals(2, countSeqByStream(INPUT, "string."));
71+
}
72+
73+
@Test
74+
void whenUsingApacheCommonsLangCountMatches_thenCorrect() {
75+
assertEquals(3, StringUtils.countMatches(INPUT, "string"));
76+
assertEquals(2, StringUtils.countMatches(INPUT, "string."));
77+
}
78+
}

core-java-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
<module>core-java-string-algorithms-3</module>
244244
<module>core-java-string-algorithms-4</module>
245245
<module>core-java-string-algorithms-5</module>
246+
<module>core-java-string-algorithms-6</module>
246247
<module>core-java-string-apis</module>
247248
<module>core-java-string-apis-2</module>
248249
<module>core-java-swing</module>

0 commit comments

Comments
 (0)