1. 简介

在本篇快速教程中,我们将学习如何在 Groovy 中从字符串中移除前缀。

首先,我们会看看 String 类提供了哪些方法来实现这个功能。接着,我们会介绍如何使用正则表达式来完成这一操作。

2. 使用 String 方法

通常来说,Groovy 被认为是 Java 生态中的动态语言。因此,我们可以继续使用 Java 中 String 类的所有方法,同时还能使用 Groovy 新增的一些方法。不过,遗憾的是,目前还没有一个像 removePrefix() 这样直接的方法来移除前缀。

Groovy 中移除字符串前缀的操作分为两个步骤:先确认前缀是否存在,再进行移除。这两个步骤可以通过 StringGroovyMethods 类提供的多种字符串操作工具方法来完成。

2.1. startsWith() 方法

startsWith() 方法用于判断一个字符串是否以指定的前缀开头。如果存在该前缀,则返回 true,否则返回 false

我们从一个 Groovy closure 开始:

def "whenCasePrefixIsRemoved_thenReturnTrue"() {
    given:
    def trimPrefix = {
        it.startsWith('Groovy-') ? it.minus('Groovy-') : it
    }

    when:
    def actual = trimPrefix("Groovy-Tutorials at Baeldung")
    def expected = "Tutorials at Baeldung"

    then:
    expected == actual
}

确认前缀存在后,我们也可以使用 substring() 方法来移除它:

trimPrefix.substring('Groovy-'.length())

2.2. startsWithIgnoreCase() 方法

⚠️ 注意:startsWith() 方法是大小写敏感的。如果你希望忽略大小写,需要手动使用 toLowerCase()toUpperCase() 方法。

startsWithIgnoreCase() 方法顾名思义,就是忽略大小写地检查前缀是否存在。如果前缀存在则返回 true,否则返回 false

来看一个使用示例:

@Test
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() 方法

上面的方法适用于只检查一个前缀的情况。如果需要检查多个前缀,Groovy 也提供了支持。

使用 startsWithAny() 方法可以检查字符串是否以任意一个指定的前缀开头。确认前缀后,我们可以根据需求进行处理:

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

3. 使用正则表达式(Regex)

正则表达式是一种强大的模式匹配和替换工具。Groovy 提供了 模式操作符 ~,可以方便地创建 java.util.regex.Pattern 实例。

定义一个简单的正则表达式来移除前缀:

def "whenPrefixIsRemovedUsingRegex_thenReturnTrue"() {
    given:
    def regex = ~"^([Gg])roovy-"
    String trimPrefix = "Groovy-Tutorials at Baeldung"

    when:
    String actual = trimPrefix - regex
    def expected = "Tutorials at Baeldung"

    then:
    expected == actual
}

如果希望忽略大小写,可以使用如下正则表达式:

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

其中,^ 表示匹配字符串的开头。

3.1. replaceFirst() 方法

结合正则表达式和原生字符串方法,我们可以实现更强大的功能。replaceFirst() 方法就是其中之一,它会替换第一个匹配正则表达式的子串。

来看一个示例:

def "whenPrefixIsRemovedUsingReplaceFirst_thenReturnTrue"() {
    given:
    def regex = ~"^groovy"
    String trimPrefix = "groovyTutorials at Baeldung's groovy page"

    when:
    String actual = trimPrefix.replaceFirst(regex, "")
    def expected = "Tutorials at Baeldung's groovy page"

    then:
    expected == actual
}

3.2. replaceAll() 方法

replaceFirst() 类似,replaceAll() 方法也接受正则表达式和替换字符串。✅ 它会替换所有匹配的子串。因此,也可以用来移除前缀。

不过要注意的是,如果只想替换开头的前缀,需要在正则中明确指定:

def "whenPrefixIsRemovedUsingReplaceAll_thenReturnTrue"() {
    given:
    String trimPrefix = "groovyTutorials at Baeldung groovy"

    when:
    String actual = trimPrefix.replaceAll(/^groovy/, "")
    def expected = "Tutorials at Baeldung groovy"

    then:
    expected == actual
}

4. 总结

在这篇教程中,我们探讨了几种从字符串中移除前缀的方法。我们不仅学会了如何处理大小写敏感的情况,还了解了如何从多个候选前缀中判断是否存在匹配项。

此外,我们也演示了多种移除子串的方法,包括使用 minus 操作符、substring 以及正则表达式相关的替换方法。

一如既往,本文所有代码示例都可以在 GitHub 上找到。


原始标题:How to Remove a Prefix From Strings in Groovy