Forum Discussion

richie's avatar
richie
Community Hero
6 years ago

Using Groovy's Split() to grab a specific string value

Hi,   Whenever I need to parse a string to grab a specific value from within the string I use this thing that counts from the right hand side and you just specify which chars you want to grab - e...
  • nmrao's avatar
    6 years ago

    richie 

     

    You can  run this script online

     

    def str = 'https://whatevs.azurewebsites.net/path?token=eyJ0eXAiOiJKV1Qi&value=123456789'
    def map = new URL(str).query?.split('&').collectEntries{ [(it.split('=').first()): it.split('=').last()]}
    log.info map.token
    log.info map.value
  • avidCoder's avatar
    avidCoder
    6 years ago

    richie 

    You can do this way also:-

     

    def fullURL = "https://whatevs.azurewebsites.net/?token=eyJ0eXAiOiJKV1Qi&value=123456789";
    String[] val = fullURL.split('&')
        for (String exp : val) {
            log.info exp.split('=')[1]
        }

    It's just a suggestion. But nmrao solution would be the best approach to use proper groovy and coding standards.