I want the pattern for removing the &b=128&f=norefer from following url

后端 未结 3 1896
遇见更好的自我
遇见更好的自我 2020-12-22 11:45

http://www.yahoo.com &b=128&f=norefer

I want to remove &b=128&f=norefer

String finalUrl =decodedUrl.repl         


        
相关标签:
3条回答
  • 2020-12-22 12:07

    you should be using URL object of java for this : http://download.oracle.com/javase/1.4.2/docs/api/java/net/URL.html#getHost()
    [search for getHost() method in it.]

    0 讨论(0)
  • 2020-12-22 12:21

    You can use the following regex replacement to remove everything after the first ampersand:

    "http://www.yahoo.com &b=128&f=norefer".replaceAll("&.*$", "");
    
    0 讨论(0)
  • 2020-12-22 12:29

    Is the reason for not wanting a hardcoded string that you wish to be able to remove some other string as well? Then you could consider writing a method like:

    public String removeNoise(String url, String noisePattern) {
        return url.replace(noisePattern, "");
    }
    
    0 讨论(0)
提交回复
热议问题