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?
Use the String. split() method with array destructuring to split a string only on the first occurrence of a character, e.g. const [first, ... rest] = str. split('-'); .
To split a string with dot, use the split() method in Java. str. split("[.]", 0);
The Groovy community has added a take() method which can be used for easy and safe string truncation. Both take() and drop() are relative to the start of the string, as in "take from the front" and "drop from the front".
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
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With