2014-12-03 69 views
11

我有像這樣的URL字符串:什麼是最好的方式擺脫從url字符串獲取參數?

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

我需要得到這個網址不帶參數,所以結果應該是:

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

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

是否有更好的方法來做到這一點:

我已經這樣做了嗎?

+2

的[從查詢字符串中刪除請求參數(可能重複http://stackoverflow.com/questions/24780016/remove - 請求參數的查詢字符串) – user23123412 2014-12-03 08:46:04

+2

不,那些是關於刪除單個參數... – Maksym 2014-12-03 08:47:59

回答

17

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

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(); 
} 
+2

使用正確的工具進行工作,而不是專注於手動串擾的微優化 - 唯一正確的答案IMO。 – Gimby 2014-12-03 09:09:22

+0

是的,你說得很對,但無論如何它取決於任務.... – Maksym 2014-12-03 09:19:50

+1

但是區別在於巨大的,索引+子串的速度提高了55倍,但並不是那麼安全。所以如果你需要性能,你需要使用indexof +子串,如果不是,那麼你應該更多的汽車關於安全。 – Maksym 2014-12-03 09:22:53

7

我通常使用

url.split("\\?")[0] 
1

嘗試使用串並的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"); 
} 
+0

是的,我做了完全一樣的方式,你認爲這是做到這一點的最佳方式? – Maksym 2014-12-03 08:51:39

+0

我的更好,如果「?「是不是在那裏你得到一個索引的憤怒 – PbxMan 2014-12-03 08:52:09

+0

總是在時間和空間之間進行權衡,我認爲如果你遇到」?「,你最終會創建兩個不同的對象。不需要在你的情況下,因此我認爲分裂將是開銷 – SMA 2014-12-03 08:59:06

1

有了它,可以通過方法來完成的URL。帶有一個字符串:

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

這將試圖以一個問號開始。當沒有問號時,保留原始字符串。

1

好吧,我會總結:

測試代碼:

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)); 
+0

所以最快的方法是str.substring(0,str.indexOf('?'))。感謝大家。 – Maksym 2014-12-03 11:11:49

+0

爲什麼第二個幾乎比第三個慢兩倍?它在做什麼額外的工作? – Jayen 2017-05-19 06:44:33

+0

爲什麼這是被接受的答案? OP從未表示過目標是績效,也沒有表明他正在緊密循環中進行數百萬次這樣的操作。 最後,這甚至不是一個正確的基準測試方式 - IANAE,但沒有預熱,JIT會扭曲所有結果。參見JMH進行適當的基準測試。 – vitaly 2017-09-03 05:30:27

1

您可以使用類似下面的內容,從URL中刪除查詢部分。

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

或者,你可能要檢查,如果URL重寫覆蓋您的需求:http://tuckey.org/urlrewrite/

相關問題