I am currently trying to split a string 1128-2
so that I can have two separate values. For example, value1: 1128 and value2: 2, so that I can then use each value separately. I have tried split()
but with no success. Is there a specific way Grails handles this, or a better way of doing it?
Asked
Active
Viewed 3.5e+01k times
111

Daniel Werner
- 1,350
- 16
- 26

thehoule64
- 1,761
- 5
- 15
- 22
-
I'm curious why this didn't work with just split? (a,b)="a-b".split("-") works fine for me (And I believe it would work in Java as well). I use it all the time. EDIT: Just looking at it they are slightly different--split returns an array while tokenize returns an ArrayList. Virtually the same thing in Groovy, the split has the advantage that it ports easily to Java, I don't think tokenize is a java method on String (unless it's a fairly new one and I missed it) – Bill K Sep 01 '16 at 16:55
6 Answers
203
Try:
def (value1, value2) = '1128-2'.tokenize( '-' )

tim_yates
- 167,322
- 27
- 342
- 338
-
39This is a good read on `split` vs `tokenize` http://www.tothenew.com/blog/groovy-tokenize-vs-split/ – Snekse Dec 23 '15 at 17:02
-
8Oh, and be careful if you're splitting on certain characters like a pipe `|`. You will need to escape the char http://stackoverflow.com/questions/3842537/grails-splitting-a-string-that-contains-a-pipe – Snekse Dec 23 '15 at 17:19
-
3
76
How are you calling split
? It works like this:
def values = '1182-2'.split('-')
assert values[0] == '1182'
assert values[1] == '2'

ataylor
- 64,891
- 24
- 161
- 189
-
21I think It's very useful to add an edge case that you might run into when spliting by the '.' char. So you'll must need to escape the dot if you want to split on a literal dot: `String extensionRemoved = filename.split("\\.")[0];` Otherwise you are splitting on the regex ., which means "any character". Note the double backslash needed to create a single backslash in the regex. – Exequiel Barrirero Jun 13 '18 at 19:10
25
def (value1, value2) = '1128-2'.split('-')
should work.
Can anyone please try this in Groovy Console?
def (v, z) = '1128-2'.split('-')
assert v == '1128'
assert z == '2'

dmahapatro
- 49,365
- 7
- 88
- 117
-
2I had problem with that, if you want to split on some type of regex chars like . or *. You need to escape it, the tokenize works in those cases too. – chandank Apr 02 '19 at 19:24
10
You can also do:
Integer a = '1182-2'.split('-')[0] as Integer
Integer b = '1182-2'.split('-')[1] as Integer
//a=1182 b=2

davidddp
- 561
- 7
- 12
4
split doesn't work that way in groovy. you have to use tokenize...
See the docs:

Gustavo Barbosa
- 1,340
- 1
- 17
- 27

Angstrom Beebe
- 67
- 1
-
split works fine actually , the understand is wrong , please see the below , String[] split_Comma_ByProperty = Stringyouwanttosplit.split(","); – sathya Apr 01 '20 at 02:10
0
dependencies {
compile ('org.springframework.kafka:spring-kafka-test:2.2.7.RELEASE') { dep ->
['org.apache.kafka:kafka_2.11','org.apache.kafka:kafka-clients'].each { i ->
def (g, m) = i.tokenize( ':' )
dep.exclude group: g , module: m
}
}
}

qwas
- 1
-
1Hello! While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian61354270 Apr 24 '20 at 13:34