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.g.

def extractedStateParmValue = locationHeaderValue[-249..-205]

//so above, counting from the right hand side, I'm grabbing the string between characters 205 and 249

This works fine - UNLESS the string length changes - if it does then the above doesn't work obviously

 

I've noticed you guys using split() quote often - but typically when I've seen it, it's been embedded within lots of other stuff - and I've never been able to unpick exactly what your code is doing.

 

I've been doing some reading and I've got some of it working.

 

Say I have the following value stored in a properties step:

 

Property Name = URL 

Property Value = https://whatevs.azurewebsites.net/?token=eyJ0eXAiOiJKV1Qi&value=123456789

so far I've got the following that grabs the value after the first = sign

 

//next line parses the Properties step and gets the value associated with the URL property
def fullURL = testRunner.testCase.getTestStepByName("Properties").getPropertyValue("URL")

def extractedValue = fullURL.split('=')[1] // [1] grabs the value AFTER the '=' sign, [0] would grab the value before '=' sign
log.info(extractedValue)

BUT I would like to use split() to grab the value AFTER the SECOND = sign.

 

I also would like to be able to grab a specific value - say between the 2 '=' signs

 

Can anyone please advise?

 

Cheers!  (having a lot of fun with this - Iove learning new stuff :))

 

rich

  • 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
  • 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.

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    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
  • nmrao's avatar
    nmrao
    Champion Level 3
    What is the use case?
    Usually, need to have some pattern, then it would be easy.
    • avidCoder's avatar
      avidCoder
      Super Contributor

      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.

      • richie's avatar
        richie
        Community Hero

        Hey lads avidCoder  & nmrao 

         

        thanks to both - as always - I reallly appreciate the help!

         

        Cheers!

         

        rich