1

I am using groovy in jenkins and there is this string that i need to get the last word in it. Let just say the string is STATUS = "EXECUTE SIT". So what I did was to split the string so that I will get an array STATUS.split() and then pop so that i can get SIT string STATUS.split().pop(). But it returns an error No signature of method: java.lang.String.pop() I am not that familiar with Groovy or Java so I don't really know if I'm using these methods correctly.

3
  • I would recommend you assign the split to an array: String[] tokens = STATUS.split() then do the pop tokens.pop() Commented Jan 8, 2020 at 3:04
  • 1
    @MadaManu An array has a pop method? Commented Jan 8, 2020 at 3:28
  • You're right! No pop on array! :) Commented Jan 8, 2020 at 3:32

1 Answer 1

3

I'm pretty sure the error you're getting is actually No signature of method: [Ljava.lang.String;.pop() is applicable for argument types: () values: []. Note the [L - this means you have an array, not a List.

split() returns an array of Strings (String[]) which doesn't support using pop() or many other of Groovy's enhancements.

Instead use tokenize() which returns a List. Once you have a list, you can use pop() or more appropriately, last(). pop() was changed in Groovy 2.5 to remove the first item instead of the last. Use removeLast() if you actually want to remove the last item.

"EXECUTE SIT".tokenize().last()
===> SIT
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.