Remove prefix from string using groovy (BAEL-4104)

Remove prefix from string using groovy (BAEL-4104)
This commit is contained in:
Usman Mohyuddin 2020-08-29 22:31:40 +05:00
parent 1e167f7fa4
commit e92ecf211a
3 changed files with 138 additions and 0 deletions

View File

@ -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)

View File

@ -0,0 +1,72 @@
<?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"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.baeldung</groupId>
<modelVersion>4.0.0</modelVersion>
<artifactId>core-groovy-strings</artifactId>
<version>1.0-SNAPSHOT</version>
<name>core-groovy-strings</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<scriptSourceDirectory>src/main/groovy</scriptSourceDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>${groovy.compiler.version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler.plugin.version}</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>${groovy.compiler.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>${groovy.version}-01</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<properties>
<junit.platform.version>1.0.0</junit.platform.version>
<groovy-wslite.version>1.1.3</groovy-wslite.version>
<groovy.version>2.5.7</groovy.version>
<surefire.plugin.version>2.20.1</surefire.plugin.version>
<compiler.plugin.version>3.8.0</compiler.plugin.version>
<groovy.compiler.version>3.3.0-01</groovy.compiler.version>
<java.version>1.8</java.version>
</properties>
</project>

View File

@ -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)
}
}