diff --git a/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/endswithpattern/StringEndsWithPattern.java b/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/endswithpattern/StringEndsWithPattern.java new file mode 100644 index 0000000000..345ad6d91f --- /dev/null +++ b/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/endswithpattern/StringEndsWithPattern.java @@ -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); + } + +} diff --git a/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/endswithpattern/StringEndsWithPatternUnitTest.java b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/endswithpattern/StringEndsWithPatternUnitTest.java new file mode 100644 index 0000000000..1680df17df --- /dev/null +++ b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/endswithpattern/StringEndsWithPatternUnitTest.java @@ -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)); + } + +}