Remove prefix from string in Groovy
I dont know much about Groovy but here is my take on this one:
def reg = ~/^groovy/ //Match 'groovy' if it is at the beginning of the String
String str = 'library-groovy' - reg
println(str)
This is case sensitive and doesn't use a regular expression:
def prefix = 'Groovy';
def string = 'Groovy1234';
def result = '';
if (string.startsWith(prefix)) {
result = string.substring(prefix.size())
print result
}
This version is plain and simple, but it meets the requirements and is an incremental change to your original:
def trimGroovy = {
it.startsWith('groovy') ? it - 'groovy' : it
}
assert "Version" == trimGroovy("groovyVersion")
assert "" == trimGroovy("groovy")
assert "spock" == trimGroovy("spock")
assert "library-groovy" == trimGroovy("library-groovy")