从url字符串中删除get参数的最佳方法是什么? [英] What's the best way to get rid of get parameters from url string?

查看:131
本文介绍了从url字符串中删除get参数的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网址字符串:

http://www.xyz/path1/path2/path3?param1=value1&param2=value2

我需要获取没有参数的url,因此结果应为:

I need to get this url without parameters, so the result should be:

http://www.xyz/path1/path2/path3

我这样做了:

private String getUrlWithoutParameters(String url)
{
  return url.substring(0,url.lastIndexOf('?'));
}

还有更好的办法吗?

推荐答案

可能不是最有效的方式,但更安全类型:

Probably not the most efficient way, but more type safe :

private String getUrlWithoutParameters(String url) throws URISyntaxException {
    URI uri = new URI(url);
    return new URI(uri.getScheme(),
                   uri.getAuthority(),
                   uri.getPath(),
                   null, // Ignore the query part of the input url
                   uri.getFragment()).toString();
}

这篇关于从url字符串中删除get参数的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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