2013-02-28 95 views
0

我需要一個java庫,它可以很容易地編輯URL。 URL以字符串的形式給出,並且必須以字符串形式返回。從url中刪除key = value

我需要的東西,如刪除key=value對,並保持網址清潔,並妥善放置?s和&。

+2

我會建議您熟悉與Java [正則表達式和模式匹配(http://docs.oracle.com/javase/tutorial/ essential/regex/pattern.html)字符串的功能。這裏可能不需要專門的圖書館。 – RudolphEst 2013-02-28 14:14:31

回答

2

使用regular expressions and pattern matching

例如:

String original = "http://www.someHost.com/somePage?key1=value1&key2=value2"; 
Pattern keyValPattern = Pattern.compile("\\p{Alpha}\\w+=[^&]+"); 
Matcher m = keyValPattern.matcher(original); 
m.find(); // find an occurence of key=value pair 
String keyVal = m.group(); // get the value of the found pair 
// keyVal will be 'key1=value1' 
int start = m.start(); // the start index of 'key1=value1' in the original string 
int end = m.end(); //the end index of 'key1=value1' in the original string 
m.find(); 
String keyVal2 = m.group();// keyVal2 will be 'key2=value2' 
// ... etc 
0

使用Java URL類來解析原始字符串並更改參數,然後只需toString()它再次爲您的結果。

0

這很簡單utility class它可以幫助。我不會僅僅爲此而使用整個庫,更好的做法是看你需要什麼,並用你需要的方法創建你自己的工具類。