Pushpraj Singh
Pushpraj Singh

Reputation: 61

How to capture the specific text inside the URL?

I have capture the current URL on the page. using :

String url = driver.getCurrentUrl();

Now I want a specific text inside this string. Let say

String url = http://www.youtube.com/watch?v=R5-gtsdenpE

and I want

  1. Capture and store the current page URL in String URL. (Done)
  2. Capture the text in the URL after "v" and store it in String emb. (??)

I am using JAVA to write my scripts on Ubuntu.

Upvotes: 0

Views: 132

Answers (4)

Ezhil V
Ezhil V

Reputation: 882

Use:

 String fullURL = http://www.youtube.com/watch?v=R5-gtsdenpE;
 String emb = fullURL.split("\\?v=")[1];

Upvotes: 1

Milesh
Milesh

Reputation: 271

If you are requirement is static and you are sure that you have to get the value after "v" than you can try this also

String emb  = url.substring(url.indexOf("v"), url.length()).trim();

Upvotes: 0

Roman C
Roman C

Reputation: 1

Url url = new Url(driver.getCurrentUrl());
Map<String, String[]> params = parameterMapFromString(url.getQuery());
String v = params.get("v")[0];

Upvotes: 0

Sudhanshu Umalkar
Sudhanshu Umalkar

Reputation: 4202

This is what you want I guess-

    String string = "http://www.youtube.com/watch?v=R5-gtsdenpE";
    URL url = new URL(string);
    System.out.println(url.getQuery());

Handle the exception appropriately.

In case you don't want to use URL class, just search for the first index of ? and then use substring() to get the string after that.

    System.out.println(string.substring(string.indexOf("?")+1));

Upvotes: 0

Related Questions