Groovy extract substring before character
I think, given the criteria as stated, that the following would give correct results in places where the chosen solution would not:
String stringParser(String inputString) {
inputString ? inputString.split(/ v\d/)[0] : ''
}
Some sample tests:
assert stringParser('John Kramer v1.1') == 'John Kramer'
assert stringParser('Kramer v Kramer v9.51') == 'Kramer v Kramer'
assert stringParser('A very hungry caterpillar v2.6') == 'A very hungry caterpillar'
You can simply extract the substring:
input.substring(0, input.lastIndexOf(" v"))
To get the part after the v
:
input.substring(input.lastIndexOf(" v") + 2)