How to extract the relative url from the absolute url in Java -


i have website:

https://asd.com/somestuff/another.html 

and want extract relative part out of it:

somestuff/another.html 

how do that?

edit: offered answer question, problem there build absolute url out of relative not i'm interested in.

you use getpath() method of url object:

url url = new url("https://asd.com/somestuff/another.html"); system.out.println(url.getpath());  // prints "/somestuff/another.html" 

now, brings actual path. if need more information (the anchor or parameters passed values), need call other accessors of url object:

url url = new url("https://asd.com/somestuff/another.html?param=value#anchor"); system.out.println(url.getpath());  // prints "/somestuff/another.html" system.out.println(url.getquery()); // prints "param=value" system.out.println(url.getref());   // prints "anchor" 

a possible use generate relative url without code, based on hiru's answer:

url absolute = new url(url, "/"); string relative = url.tostring().substring(absolute.tostring().length()); system.out.println(relative); // prints "somestuff/another.html?param=value#anchor" 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

c# - Exception when attempting to modify Dictionary -