mindparse
mindparse

Reputation: 7225

Jenkins Pipeline - Groovy string function to remove trailing comma

Probably a super easy one to answer, but in a a Jenkins Pipeline script, what string function can I use to strip off a trailing comma from a string I have defined.

For example:

def fooBar = 'foo, bar,'

Thanks

Upvotes: 0

Views: 1779

Answers (1)

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27210

If you know the comma is the last character, as in the hardcoded literal that referenced in the question, you could do something like this:

def fooBar = 'foo, bar,'
def result = fooBar[0..-2]

If you don't know the comma is the last character, you could find it by doing something like fooBar.lastIndexOf(',') and use that value to retrieve the characters before and after the comma and concatenate them.

Upvotes: 1

Related Questions