Reputation: 635
I got a string like this:
params.search = abc def 123
in the string, name: abc and position: def 123
params.search.split(" ").each{ key->
ilike ('name', key[0].toString())
ilike ('position', ....
}
My problem is that how can I remove the name in string (first index of string), then set the rest of string for position in ilike()
Upvotes: 0
Views: 461
Reputation: 50245
I suppose you are looking for name
to be abc
and position
to be def 123
?
def string = "abc def 123"
def splitStr = string.split()
def name = splitStr[0]
def position = splitStr[1..-1].join(/ /)
assert name == 'abc'
assert position == 'def 123'
UPDATE
I would rather use @Tim's approach because above approach is verbose.
Using split(regex, limit)
and multiple assignment makes the implementation groovier and smarter.
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.
That answer is the best possible answer to the question and should be accepted instead. :)
Upvotes: 2
Reputation: 171074
You could also use multiple assignment and limit the results from split
:
def str = "abc def 123"
def (name,position) = str.split( ' ', 2 )
assert name == 'abc'
assert position == 'def 123'
Upvotes: 2