Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


1. Introduction

In this quick tutorial, we'll learn how to remove the prefix from a string in Groovy.

First, we'll look at what the String class offers for this purpose. After that, we'll move on to regular expressions and see how we can use them to remove a prefix.

2. Using String Methods

Generally, Groovy is considered a dynamic language for the Java ecosystem. Therefore, we can still use every Java String class method along with new Groovy ones. However, for the removal of the prefix, there is still an absence of a straightforward method like removePrefix().

Removing of a prefix from Groovy strings consists of two steps: first confirmation and then removal. Both of these steps can be performed using the StringGroovyMethods class that offers many utility methods for string manipulations.

2.1. startsWith() Method

The startWith() method tests if a string starts with a specific prefix. It returns true if the prefix exists and false otherwise.

Let's start with a groovy closure:

public void whenCasePrefixIsRemoved_thenReturnTrue(){
    def trimPrefix = {
        it.startsWith('Groovy-') ? it.minus('Groovy-') : it 
    def actual = trimPrefix("Groovy-Tutorials at Baeldung")
    def expected = "Tutorials at Baeldung"
    assertEquals(expected, actual)

Once existence is confirmed, then we can also use the substring() method to remove it:


2.2. startsWithIgnoreCase() Method

The startsWith() method is case-sensitive. So, a manual effort is required to negate the effect of the case either by applying the toLowerCase() or toUpperCase() methods.

As the name suggests, the startsWithIgnoreCase() searches a prefix without case consideration. It returns true if a prefix exists and false otherwise.

Let's look at how to use this method:

public void whenPrefixIsRemovedWithIgnoreCase_thenReturnTrue() {

    String prefix = "groovy-"
    String trimPrefix = "Groovy-Tutorials at Baeldung"
    def actual
    if(trimPrefix.startsWithIgnoreCase(prefix)) {
        actual = trimPrefix.substring(prefix.length())

    def expected = "Tutorials at Baeldung"

    assertEquals(expected, actual)

2.3. startsWithAny() Method

The above solutions are useful when we have to check only one prefix. When it comes to checking multiple prefixes, Groovy also provides support to check multiple prefixes.

The startsWithAny() method checks if the CharSequence starts with any specified prefixes. Once the prefix is confirmed, we can apply logic according to requirements:

String trimPrefix = "Groovy-Tutorials at Baeldung"
if (trimPrefix.startsWithAny("Java", "Groovy", "Linux")) {
    // logic to remove prefix

3. Using Regex

A regular expression is a powerful way to match or replace a pattern. Groovy has a pattern operator ~ that provides a simple way to create a java.util.regex.Pattern instance.

Let's define a simple regular expression to remove a prefix:

public void whenPrefixIsRemovedUsingRegex_thenReturnTrue() {

    def regex = ~"^groovy-"
    String trimPrefix = "groovy-Tutorials at Baeldung"
    String actual = trimPrefix - regex

    def expected = "Tutorials at Baeldung"
    assertEquals("Tutorials at Baeldung", actual)

The case-insensitive version of the above regular expression:

def regex = ~"^([Gg])roovy-"

The caret operator ^ will make sure that the specified substring exists at the start.

3.1. replaceFirst() Method

Using regular expressions along with native strings methods, we can perform very powerful tricks. The replaceFirst() method is one of these methods. It replaces the first occurrence that matches the given regular expression.

Let's remove a prefix using the replaceFirst() method:

public void whenPrefixIsRemovedUsingReplaceFirst_thenReturnTrue() {

    def regex = ~"^groovy"
    String trimPrefix = "groovyTutorials at Baeldung's groovy page"
    String actual = trimPrefix.replaceFirst(regex, "")

    def expected = "Tutorials at Baeldung's groovy page"
    assertEquals(expected, actual)

3.2. replaceAll() Method

Just like replaceFirst(), the replaceAll() also accepts a regular expression and given replacement. It replaces each substring that matches the given criteria. To remove a prefix, we can use this method too.

Let's use replaceAll() to replace a substring at the start of the string only:

public void whenPrefixIsRemovedUsingReplaceAll_thenReturnTrue() {

    String trimPrefix = "groovyTutorials at Baeldung groovy"
    String actual = trimPrefix.replaceAll(/^groovy/, "")

    def expected = "Tutorials at Baeldung groovy"
    assertEquals(expected, actual)

4. Conclusion

In this quick tutorial, we explored several ways to remove prefixes from a string. To confirm the existence of a prefix, we saw how to do this for both uppercase and lowercase strings.

At the same time, we've seen how to detect a prefix among many provided substrings. We also looked at multiple methods that can be used to remove a substring. Lastly, we briefly discussed the role of regex for this purpose.

As always, all the code examples can be found over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Comments are closed on this article!