init commit (#11643)
This commit is contained in:
parent
09e0fa4e74
commit
b6cff5acd4
|
@ -0,0 +1,45 @@
|
||||||
|
package com.baeldung.endswithpattern;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
public class StringEndsWithPattern {
|
||||||
|
|
||||||
|
public static boolean usingStringEndsWithMethod(String text, String suffix) {
|
||||||
|
if (text == null || suffix == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return text.endsWith(suffix);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean usingStringMatchesMethod(String text, String suffix) {
|
||||||
|
if (text == null || suffix == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String regex = ".*" + suffix + "$";
|
||||||
|
return text.matches(regex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean usingStringRegionMatchesMethod(String text, String suffix) {
|
||||||
|
if (text == null || suffix == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int toffset = text.length() - suffix.length();
|
||||||
|
return text.regionMatches(toffset, suffix, 0, suffix.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean usingPatternClass(String text, String suffix) {
|
||||||
|
if (text == null || suffix == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Pattern pattern = Pattern.compile(".*" + suffix + "$");
|
||||||
|
return pattern.matcher(text)
|
||||||
|
.find();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean usingApacheCommonsLang(String text, String suffix) {
|
||||||
|
return StringUtils.endsWith(text, suffix);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.baeldung.endswithpattern;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class StringEndsWithPatternUnitTest {
|
||||||
|
|
||||||
|
private static final String TEXT = "Welcome to baeldung.com";
|
||||||
|
private static final String SUFFIX = "com";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenStringAndSuffix_whenUsingStringEndsWith_thenCheck() {
|
||||||
|
|
||||||
|
assertTrue(StringEndsWithPattern.usingStringEndsWithMethod(TEXT, SUFFIX));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenStringAndSuffix_whenUsingStringMatches_thenCheck() {
|
||||||
|
|
||||||
|
assertTrue(StringEndsWithPattern.usingStringMatchesMethod(TEXT, SUFFIX));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenStringAndSuffix_whenUsingStringRegionMatches_thenCheck() {
|
||||||
|
|
||||||
|
assertTrue(StringEndsWithPattern.usingStringRegionMatchesMethod(TEXT, SUFFIX));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenStringAndSuffix_whenUsingPatternClass_thenCheck() {
|
||||||
|
|
||||||
|
assertTrue(StringEndsWithPattern.usingPatternClass(TEXT, SUFFIX));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenStringAndSuffix_whenUsingApacheCommonsLang_thenCheck() {
|
||||||
|
|
||||||
|
assertTrue(StringEndsWithPattern.usingApacheCommonsLang(TEXT, SUFFIX));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue