如何使用JSOUP或Coldfusion从URL中删除查询字符串和哈希值?

时间:2015-11-02 06:11:30

标签: java coldfusion jsoup

1 个答案:

答案 0 :(得分:3)

我创建了一个帮助方法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