diff --git a/core-groovy-strings/README.md b/core-groovy-strings/README.md new file mode 100644 index 0000000000..e3202e2dd0 --- /dev/null +++ b/core-groovy-strings/README.md @@ -0,0 +1,8 @@ +# Core Groovy Strings + +This module contains articles about core Groovy strings concepts + +## Relevant articles: + +- [Remove prefix in Groovy]() +- [[<-- Prev]](/core-groovy-strings) diff --git a/core-groovy-strings/pom.xml b/core-groovy-strings/pom.xml new file mode 100644 index 0000000000..059b53662c --- /dev/null +++ b/core-groovy-strings/pom.xml @@ -0,0 +1,72 @@ + + + com.baeldung + 4.0.0 + core-groovy-strings + 1.0-SNAPSHOT + core-groovy-strings + jar + + + + org.codehaus.groovy + groovy-all + ${groovy.version} + pom + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + + + + + src/main/groovy + src/main/java + + + org.codehaus.groovy + groovy-eclipse-compiler + ${groovy.compiler.version} + true + + + maven-compiler-plugin + ${compiler.plugin.version} + + groovy-eclipse-compiler + ${java.version} + ${java.version} + + + + org.codehaus.groovy + groovy-eclipse-compiler + ${groovy.compiler.version} + + + org.codehaus.groovy + groovy-eclipse-batch + ${groovy.version}-01 + + + + + + + + 1.0.0 + 1.1.3 + 2.5.7 + 2.20.1 + 3.8.0 + 3.3.0-01 + 1.8 + + + \ No newline at end of file diff --git a/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy b/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy new file mode 100644 index 0000000000..568e5fdde8 --- /dev/null +++ b/core-groovy-strings/src/test/groovy/com/baeldung/removeprefix/RemovePrefixTest.groovy @@ -0,0 +1,58 @@ +package com.baeldung.removeprefix + +import org.junit.Assert +import org.junit.Test + +class RemovePrefixTest { + + + @Test + public void givenWhenCasePrefixIsRemoved_thenReturnTrue() { + def trimPrefix = { + it.startsWith('Groovy') ? it.minus('Groovy') : it + } + + Assert.assertEquals(trimPrefix("GroovyTutorials at Baeldung"), "Tutorials at Baeldung") + } + + @Test + public void givenWhenPrefixIsRemoved_thenReturnTrue() { + + String prefix = "groovy-" + String trimPrefix = "Groovy-Tutorials at Baeldung" + def result; + if(trimPrefix.startsWithIgnoreCase(prefix)) { + result = trimPrefix.substring(prefix.length()) + } + + Assert.assertEquals("Tutorials at Baeldung", result) + } + + @Test + public void givenWhenPrefixIsRemovedUsingRegex_thenReturnTrue() { + + def regex = ~"^([Gg])roovy-" + String trimPrefix = "Groovy-Tutorials at Baeldung" + String result = trimPrefix - regex + + Assert.assertEquals("Tutorials at Baeldung", result) + } + + @Test public void givenWhenPrefixIsRemovedUsingReplaceFirst_thenReturnTrue() { + def regex = ~"^groovy" + String trimPrefix = "groovyTutorials at Baeldung's groovy page" + String result = trimPrefix.replaceFirst(regex, "") + + Assert.assertEquals("Tutorials at Baeldung's groovy page", result) + } + + @Test + public void givenWhenPrefixIsRemovedUsingReplaceAll_thenReturnTrue() { + + String trimPrefix = "groovyTutorials at Baeldung groovy" + String result = trimPrefix.replaceAll(/^groovy/, "") + + Assert.assertEquals("Tutorials at Baeldung groovy", result) + } + +}