Ask a Question

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

SOLVED
richie
Community Hero

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

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
4 REPLIES 4
nmrao
Community Hero

What is the use case?
Usually, need to have some pattern, then it would be easy.


Regards,
Rao.
nmrao
Community Hero

@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


Regards,
Rao.
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.

Hey lads @avidCoder  & @nmrao 

 

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

 

Cheers!

 

rich

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
cancel
Showing results for 
Search instead for 
Did you mean: