Splitting String with delimiter

后端 未结 6 1662
死守一世寂寞
死守一世寂寞 2021-01-30 15:35

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 se

相关标签:
6条回答
  • 2021-01-30 16:02

    You can also do:

    Integer a = '1182-2'.split('-')[0] as Integer
    Integer b = '1182-2'.split('-')[1] as Integer
    
    //a=1182 b=2
    
    0 讨论(0)
  • 2021-01-30 16:06

    split doesn't work that way in groovy. you have to use tokenize...

    See the docs:

    http://groovy-lang.org/gdk.html#split()

    0 讨论(0)
  • 2021-01-30 16:08

    How are you calling split? It works like this:

    def values = '1182-2'.split('-')
    assert values[0] == '1182'
    assert values[1] == '2'
    
    0 讨论(0)
  • 2021-01-30 16:08

    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'
    
    0 讨论(0)
  • 2021-01-30 16:10
    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
         }
       }
    }
    
    0 讨论(0)
  • 2021-01-30 16:12

    Try:

    def (value1, value2) = '1128-2'.tokenize( '-' )
    
    0 讨论(0)
提交回复
热议问题