Service Virtualization

Expand all | Collapse all

How to extract a substring of characters from a string spread across multiple lines?

Jump to Best Answer
  • 1.  How to extract a substring of characters from a string spread across multiple lines?

    Posted 08-30-2017 09:10 AM

    Greetings Team,

     

    I have created a VS, and one of request side argument say val1 has below value

     


    WABCDRR
    .XCDUTF
    FMM
    MTYTTY
    023-93071270-M
    AAA

     

    Now I need to extract a subset, i.e "023-93071270" from the incoming request and send it in the response.

     

    Do we have similar method like substring to do the same?

     

    Thanks in advance.



  • 2.  Re: How to extract a substring of characters from a string spread across multiple lines?
    Best Answer

    Posted 08-30-2017 05:47 PM

    Maddy,

     

    This scenario can very likely be addressed with one or more compound XPath expressions. However, I'm not well-versed enough in complex XPath and regular expressions to offer such an approach, so I will offer a coding approach instead.

     

    My approach will make two assumptions:

     

    1. The "subset" value to be extracted is a value of the following format: NNN-NNNNNNNN-X, where N is an integer and X is an alphabetic character

    2. The "subset" value is on the fifth line/row.

     

    First you need to use an XPath filter to extract the value using an XPath expression. If you have a smart enough expression to extract the subset value then you can enter that XPath here as well. Save the result of the XPath into a property

     

    Now that you have extracted the multi-line value and saved it into a property using the XPath filter you can add a new Execute script step with the following code to save just the subset value:

     

    // replace MyPropertyName with the actual name of the property where you saved the multi line value

    String multiLineValue = testExec.getStateValue("MyPropertyName"); 

     

    String[] values = multiLineValue.split("\n");

    // if you know the value is always in the 5th line/row/index then you can get the value directly using index 4

    String value = values[4].trim().substring(0, 12);

    testExec.setStateValue("MyNewPropertyName", value);

     

     

    // if you don't know if the value will be in the 5th line/row/index then you have to add some type of condition

    // for example let's assume the value has to be 14 characters long with a hyphen as the 4th character...then 

    for (int i = 0; i < values.length; i++) {
       String s = values[i];
       // if "-" is in the 3rd index (e.g. 4th character in the string)
       if (s.indexOf("-") == 3 && s.length() == 14) {
          String newValue = s.substring(0, 12);

          testExec.setStateValue("MyNewPropertyName", newValue);
          return;
       }
    }



  • 3.  Re: How to extract a substring of characters from a string spread across multiple lines?

    Posted 08-31-2017 05:52 AM

    Thanks William:) The solution worked...