从url字符串中删除get参数的最佳方法是什么?

我有URL字符串,如:

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

我需要获取没有参数的url,结果应该是:

“ http://www.xyz/path1/path2/path3 ”。

我这样做了:

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

有没有更好的方法呢?

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

 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.split("\\?")[0] 

使用URL可以通过方法完成。 使用字符串:

 url = url.replaceFirst("\\?.*$", ""); 

这会尝试用问号替换所有开头。 如果没有问号,则保留原始字符串。

尝试在String中使用substring和indexOf方法:

 String str = "http://www.xyz/path1/path2/path3?param1=value1&param2=value2"; int index = str.indexOf("?"); if (index != -1) { System.out.println(str.substring(0, str.indexOf("?"))); } else { System.out.println("You dont have question mark in your url"); } 

您可以使用以下内容,从URL中删除查询部分。

 private String getUrlWithoutParameters(String url) throws MalformedURLException { return url.replace(new URL(url).getQuery(), ""); } 

或者,您可能想要检查url重写是否满足您的需求: http : //tuckey.org/urlrewrite/

好的,我会总结一下:

测试代码:

 package com.stackoverflow.test; import java.net.URI; import java.net.URISyntaxException; public class TestOfRemovingParameteresFromString { private final static int NUMBER_OF_ITERATIONS = 1000000; private final static String TEST_URL_SRTRING = "http://www.xyz/path1/path2/path3?param1=value1&param2=value2"; private static String getUrlWithoutParameters(String url) throws URISyntaxException { URI uri = new URI(url); return new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, uri.getFragment()).toString(); } public static void main(String[] args) throws URISyntaxException { long startTime = System.currentTimeMillis(); for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) { String result = TEST_URL_SRTRING.split("\\?")[0]; } System.out.println("Result=" + (System.currentTimeMillis() - startTime)); startTime = System.currentTimeMillis(); for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) { String result = TEST_URL_SRTRING.substring(0, TEST_URL_SRTRING.indexOf("?")); } System.out.println("Result=" + (System.currentTimeMillis() - startTime)); startTime = System.currentTimeMillis(); for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) { String result = TEST_URL_SRTRING.substring(0, TEST_URL_SRTRING.indexOf('?')); } System.out.println("Result=" + (System.currentTimeMillis() - startTime)); startTime = System.currentTimeMillis(); for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) { String result = TEST_URL_SRTRING.substring(0, TEST_URL_SRTRING.lastIndexOf('?')); } System.out.println("Result=" + (System.currentTimeMillis() - startTime)); startTime = System.currentTimeMillis(); for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) { String result = TEST_URL_SRTRING.replaceFirst("\\?.*$", ""); } System.out.println("Result=" + (System.currentTimeMillis() - startTime)); startTime = System.currentTimeMillis(); for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) { String result = getUrlWithoutParameters(TEST_URL_SRTRING); } System.out.println("Result=" + (System.currentTimeMillis() - startTime)); } } 

输出:

结果= 353

结果= 54

结果= 29

结果= 37

结果= 1086

结果= 1647

所以最快的是第三个:

  startTime = System.currentTimeMillis(); for (int i = 0; i < NUMBER_OF_ITERATIONS; i++) { String result = TEST_URL_SRTRING.substring(0, TEST_URL_SRTRING.indexOf('?')); } System.out.println("Result=" + (System.currentTimeMillis() - startTime)); 

使用JAX-RS 2.0中的javax.ws.rs.core.UriBuilder :

 UriBuilder.fromUri("https://www.google.co.nz/search?q=test").replaceQuery(null).build(); 

使用Spring中非常相似的org.springframework.web.util.UriBuilder :

 UriComponentsBuilder.fromUriString("https://www.google.co.nz/search?q=test").replaceQuery(null).build(Collections.emptyMap());