Groovy split on period and return only first value

岁酱吖の 提交于 2020-01-14 07:21:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!