-3

To get a string of characters from the following url

http://10.10.10.14:8082/RunReport.i4?reportUUID=9803f942-81fb-4d83-a509-f38233a2b9a7&primaryOrg=1&clientOrg=1

            String CurrentUrl = driver.getCurrentUrl();
            System.out.println(CurrentUrl);


           if (CurrentUrl != null) {
            String result = CurrentUrl.substring(CurrentUrl.indexOf("=") + 1, CurrentUrl.indexOf("&"));
            System.out.println(result);

            }
Bob Jones
  • 171
  • 1
  • 4
  • 22

2 Answers2

4

There are two ways to approach this:

  • Use text pattern matching to extract the string between "reportUUID=" and the following "&". For example you could do this with Pattern and Matcher.

  • Use a URL parser to parse the URL. Then either

    • extract the "query" part and use String::split or pattern matching to extract the query parameter you want, or

    • use a 3rd-party library to do the job. Some examples are given in Parse a URI String into Name-Value Collection.

The second approach is better. The problem with the first one is that there are various ways that a URL's query part could be encoded that are liable to confound simple-minded pattern matching. A URL parser will deal with the decoding for you.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1
public class Example {
    public static void main(String[] args) throws Exception {

    String url="http://10.10.10.14:8082/RunReport.i4?reportUUID=9803f942-81fb-4d83-a509-f38233a2b9a7&primaryOrg=1&clientOrg=1";         
        try {
             Map<String, String> values = getUrlValues(url);
             String reportUUID = values.get("reportUUID");
             String primaryOrg = values.get("primaryOrg");
             String clientOrg = values.get("clientOrg");
             System.out.println("reportUUID:: "+reportUUID);
             System.out.println("primaryOrg:: "+primaryOrg);
             System.out.println("clientOrg:: "+ clientOrg);
        } catch (Exception e) {
            System.out.println("Error"+e.getMessage());
        }       
    }

    private static Map<String, String> getUrlValues(String url) {
        int i = url.indexOf("?");
        Map<String, String> paramsMap = new HashMap<>();
        if (i > -1) {
            String searchURL = url.substring(url.indexOf("?") + 1);
            String params[] = searchURL.split("&");
            for (String param : params) {
                String temp[] = param.split("=");
                try {
                    paramsMap.put(temp[0], java.net.URLDecoder.decode(temp[1], "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return paramsMap;
    }

} try this one edit && copy from stack over flow

Kamal Kumar
  • 194
  • 12