问题
I have the input as
var = primarynode.domain.local
and now I Need only primarynode
from it.
I was looking both split and tokenize but not able to do it in one line code. does anyone know how to do it in one line code?
回答1:
Well assuming that you want to just get the first word(before
.
) from the input string.
You can use the tokenize
operator of the String
If you have
def var = "primarynode.domain.local"
then you can do
def firstValue = var.tokenize(".")[0]
println firstValue
output
primarynode
回答2:
The split
method works, you just have to be aware that the argument is a regular expression and not a plain String. And since "." means "any character" in a regular expression, you'll need to escape it...
var = 'primarynode.domain.local'.split(/\./)[0]
...or use a character class (the "." is not special inside a character class)
var = 'primarynode.domain.local'.split(/[.]/)[0]
来源:https://stackoverflow.com/questions/39745839/groovy-split-on-period-and-return-only-first-value