如何使用JSOUP或Coldfusion从URL中删除查询字符串和散列值?
问题描述:
答
我创建一个辅助方法processURL()
它接受一个URL,并返回包含一切高达任查询标记(?
)或井号(#
)的URL:
String processURL(String theURL) {
int endPos;
if (theURL.indexOf("?") > 0) {
endPos = theURL.indexOf("?");
} else if (theURL.indexOf("#") > 0) {
endPos = theURL.indexOf("#");
} else {
endPos = theURL.length();
}
return theURL.substring(0, endPos);
}
String urlOne = "http://stackoverflow.com/questions/tagged/jav?#sort=featured&pageSize=50";
String urlTwo = "http://stackoverflow.com/questions/tagged/java#comments";
System.out.println(processURL(urlOne));
System.out.println(processURL(urlTwo));
输出:
http://stackoverflow.com/questions/tagged/java
http://stackoverflow.com/questions/tagged/java