在 java 中获取 URL 参数并从该 URL 中提取特定文本 [英] Getting URL parameter in java and extract a specific text from that URL

查看:28
本文介绍了在 java 中获取 URL 参数并从该 URL 中提取特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 URL,我需要从这个 URL 中获取 v 的值.这是我的网址:http://www.youtube.com/watch?v=_RCIP6OrQrE

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

我该怎么做?

推荐答案

我认为最简单的方法之一是解析 URL.getQuery() as

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.

这篇关于在 java 中获取 URL 参数并从该 URL 中提取特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆