这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions core-java-modules/core-java-string-algorithms-6/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-algorithms-6</artifactId>
<packaging>jar</packaging>
<name>core-java-string-algorithms-6</name>

<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${common-lang3.version}</version>
</dependency>
</dependencies>

<properties>
<common-lang3.version>3.18.0</common-lang3.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.baeldung.countseq;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;

public class CountSequenceInStringUnitTest {

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.";

int countSeqByIndexOf(String input, String seq) {
int count = 0;
int index = input.indexOf(seq);
while (index != -1) {
count++;
index = input.indexOf(seq, index + seq.length());
}
return count;
}

@Test
void whenUsingIndexOf_thenCorrect() {
assertEquals(3, countSeqByIndexOf(INPUT, "string"));
assertEquals(2, countSeqByIndexOf(INPUT, "string."));
}

int countSeqByRegexFind(String input, String seq) {
// Alternative: Pattern pattern = Pattern.compile(seq, Pattern.LITERAL);
Matcher matcher = Pattern.compile(Pattern.quote(seq))
.matcher(input);
int count = 0;
while (matcher.find()) {
count++;
}
return count;
}

@Test
void whenUsingRegexFind_thenCorrect() {
assertEquals(3, countSeqByRegexFind(INPUT, "string"));
assertEquals(2, countSeqByRegexFind(INPUT, "string."));
}

int countSeqByRegexSplit(String input, String seq) {
Pattern pattern = Pattern.compile(seq, Pattern.LITERAL);
return pattern.split(input, -1).length - 1;
}

@Test
void whenUsingRegexSplit_thenCorrect() {
assertEquals(3, countSeqByRegexSplit(INPUT, "string"));
assertEquals(2, countSeqByRegexSplit(INPUT, "string."));
}

int countSeqByStream(String input, String seq) {
long count = Pattern.compile(Pattern.quote(seq))
.matcher(input)
.results()
.count();
return Math.toIntExact(count);
}

@Test
void whenUsingStream_thenCorrect() {
assertEquals(3, countSeqByStream(INPUT, "string"));
assertEquals(2, countSeqByStream(INPUT, "string."));
}

@Test
void whenUsingApacheCommonsLangCountMatches_thenCorrect() {
assertEquals(3, StringUtils.countMatches(INPUT, "string"));
assertEquals(2, StringUtils.countMatches(INPUT, "string."));
}
}
1 change: 1 addition & 0 deletions core-java-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
<module>core-java-string-algorithms-3</module>
<module>core-java-string-algorithms-4</module>
<module>core-java-string-algorithms-5</module>
<module>core-java-string-algorithms-6</module>
<module>core-java-string-apis</module>
<module>core-java-string-apis-2</module>
<module>core-java-swing</module>
Expand Down