Getting URL parameter in java and extract a specific text from that URL

bynu022 picture bynu022 · Jul 31, 2012 · Viewed 127.7k times · Source

I have a URL and I need to get the value of v from this URL. Here is my URL: http://www.youtube.com/watch?v=_RCIP6OrQrE

How can I do that?

Answer

verisimilitude picture verisimilitude · Jul 31, 2012

I think the one of the easiest ways out would be to parse the string returned by URL.getQuery() as

public static Map<String, String> getQueryMap(String query) {  
    String[] params = query.split("&");  
    Map<String, String> map = new HashMap<String, String>();

    for (String param : params) {  
        String name = param.split("=")[0];  
        String value = param.split("=")[1];  
        map.put(name, value);  
    }  
    return map;  
}

You can use the map returned by this function to retrieve the value keying in the parameter name.