Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How can we get Last part of variable in a string in Soa test

tejaswini9228
tejaswini9228 Posts: 1

For ex: I have this xpath: /root/fields[1]/customefield_xxxx[1]/text()
If I evaluate this Xpath am getting 12345-1000-2000-56789
But I just need to get 56789(last part)in my
output.
I have tried using substring, last(), postion and index..but none of them working.

Answers

  • benken_parasoft
    benken_parasoft Posts: 1,309 ✭✭✭

    Based on your example, I infer your JSON looks like this:

    {
        "fields" : {
            "customefield_xxxx" : "12345-1000-2000-56789"
        }
    }
    

    In a JSON Data Bank, I can create an extraction for "customefield_xxxx" which generates the XPath "/root/fields[1]/customefield_xxxx[1]/text()".

    In XPath, there are various String functions available. W3Schools is a nice introductory reference for things like this: https://www.w3schools.com/xml/xsl_functions.asp#string

    Yes, you could use a "substring" method to get the last part like this:

    substring(/root/fields[1]/customefield_xxxx[1]/text(), 17, 5)
    

    However, this XPath may be a little brittle because of the hard coded values for "start" and "length". I've often used the "replace" function instead:

    replace(/root/fields[1]/customefield_xxxx[1]/text(), "[0-9]+-[0-9]+-[0-9]+-([0-9]+)", "$1")
    

    This way, you can define a regular expression that describes the format of the string and replace the value with some group that was matched. In this case, I put parenthesis around the part that matches the last segment which I can then reference as "$1".