Merge pull request #8534 from carloscaverobarca/BAEL-3739-string-Contains-Case-Insensitive-Java
[BAEL 3739] - Workarounds for String contains case insensitive in Java
This commit is contained in:
commit
218d9c92f4
|
@ -8,4 +8,5 @@ This module contains articles about string operations.
|
||||||
- [String Initialization in Java](https://www.baeldung.com/java-string-initialization)
|
- [String Initialization in Java](https://www.baeldung.com/java-string-initialization)
|
||||||
- [String toLowerCase and toUpperCase Methods in Java](https://www.baeldung.com/java-string-convert-case)
|
- [String toLowerCase and toUpperCase Methods in Java](https://www.baeldung.com/java-string-convert-case)
|
||||||
- [Java String equalsIgnoreCase()](https://www.baeldung.com/java-string-equalsignorecase)
|
- [Java String equalsIgnoreCase()](https://www.baeldung.com/java-string-equalsignorecase)
|
||||||
|
- [How to avoid String contains() Case Insensitive in Java](https://www.baeldung.com/how-to-avoid-string-contains-case-insensitive-in-java)
|
||||||
- More articles: [[<-- prev]](../core-java-string-operations)
|
- More articles: [[<-- prev]](../core-java-string-operations)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>core-java-string-operations-2</artifactId>
|
<artifactId>core-java-string-operations-2</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
@ -51,6 +52,18 @@
|
||||||
<version>${org.hamcrest.version}</version>
|
<version>${org.hamcrest.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-core</artifactId>
|
||||||
|
<version>${jmh-core.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
|
<artifactId>jmh-generator-annprocess</artifactId>
|
||||||
|
<version>${jmh-generator.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
|
@ -61,6 +74,29 @@
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>core-java-string-operations-2</finalName>
|
<finalName>core-java-string-operations-2</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<transformers>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<mainClass>org.openjdk.jmh.Main</mainClass>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
<resources>
|
<resources>
|
||||||
<resource>
|
<resource>
|
||||||
<directory>src/main/resources</directory>
|
<directory>src/main/resources</directory>
|
||||||
|
|
|
@ -0,0 +1,78 @@
|
||||||
|
package com.baeldung.contains;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
|
import org.openjdk.jmh.annotations.Fork;
|
||||||
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
|
import org.openjdk.jmh.annotations.Setup;
|
||||||
|
import org.openjdk.jmh.annotations.State;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Based on https://github.com/tedyoung/indexof-contains-benchmark
|
||||||
|
*/
|
||||||
|
@Fork(5)
|
||||||
|
@State(Scope.Benchmark)
|
||||||
|
@BenchmarkMode(Mode.AverageTime)
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||||
|
public class CaseInsensitiveWorkarounds {
|
||||||
|
|
||||||
|
private String src;
|
||||||
|
private String dest;
|
||||||
|
private Pattern pattern;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
org.openjdk.jmh.Main.main(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Setup
|
||||||
|
public void setup() {
|
||||||
|
src = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
|
||||||
|
dest = "eiusmod";
|
||||||
|
pattern = Pattern.compile(Pattern.quote(dest), Pattern.CASE_INSENSITIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// toLowerCase() and contains()
|
||||||
|
@Benchmark
|
||||||
|
public boolean lowerCaseContains() {
|
||||||
|
return src.toLowerCase()
|
||||||
|
.contains(dest.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
// matches() with Regular Expressions
|
||||||
|
@Benchmark
|
||||||
|
public boolean matchesRegularExpression() {
|
||||||
|
return src.matches("(?i).*" + dest + ".*");
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean processRegionMatches(String localSrc, String localDest) {
|
||||||
|
for (int i = localSrc.length() - localDest.length(); i >= 0; i--)
|
||||||
|
if (localSrc.regionMatches(true, i, localDest, 0, localDest.length()))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// String regionMatches()
|
||||||
|
@Benchmark
|
||||||
|
public boolean regionMatches() {
|
||||||
|
return processRegionMatches(src, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pattern CASE_INSENSITIVE with regexp
|
||||||
|
@Benchmark
|
||||||
|
public boolean patternCaseInsensitiveRegexp() {
|
||||||
|
return pattern.matcher(src)
|
||||||
|
.find();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apache Commons StringUtils containsIgnoreCase
|
||||||
|
@Benchmark
|
||||||
|
public boolean apacheCommonsStringUtils() {
|
||||||
|
return org.apache.commons.lang3.StringUtils.containsIgnoreCase(src, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.baeldung.contains;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BAEL-3739: Different ways to solve the contains() case insensitive behavior.
|
||||||
|
*/
|
||||||
|
public class CaseInsensitiveWorkaroundsUnitTest {
|
||||||
|
|
||||||
|
private String src = "Lorem ipsum dolor sit amet";
|
||||||
|
private String dest = "lorem";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_whenCallingContainsWithToLowerOrUpperCase_shouldReturnTrue() {
|
||||||
|
// Use toLowerCase to avoid case insensitive issues
|
||||||
|
Assert.assertTrue(src.toLowerCase().contains(dest.toLowerCase()));
|
||||||
|
|
||||||
|
// Use toUpperCase to avoid case insensitive issues
|
||||||
|
Assert.assertTrue(src.toUpperCase().contains(dest.toUpperCase()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_whenCallingStringMatches_thenReturnsTrue() {
|
||||||
|
// Use String Matches to avoid case insensitive issues
|
||||||
|
Assert.assertTrue(src.matches("(?i).*" + dest + ".*"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_whenCallingStringRegionMatches_thenReturnsTrue() {
|
||||||
|
// Use String Region Matches to avoid case insensitive issues
|
||||||
|
CaseInsensitiveWorkarounds comparator = new CaseInsensitiveWorkarounds();
|
||||||
|
Assert.assertTrue(comparator.processRegionMatches(src, dest));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_whenCallingPaternCompileMatcherFind_thenReturnsTrue() {
|
||||||
|
// Use Pattern Compile Matcher and Find to avoid case insensitive issues
|
||||||
|
Assert.assertTrue(Pattern.compile(Pattern.quote(dest),
|
||||||
|
Pattern.CASE_INSENSITIVE) .matcher(src) .find());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_whenCallingStringUtilsContainsIgnoreCase_thenReturnsTrue() {
|
||||||
|
// Use StringUtils containsIgnoreCase to avoid case insensitive issues
|
||||||
|
Assert.assertTrue(StringUtils.containsIgnoreCase(src, dest));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue