Mehandi Hassan
Mehandi Hassan

Reputation: 2601

How to remove the parameters in url Java

I have a URL when I run my project.

http://localhost:8084/blog1_1/title?uname=55%22

and I want remove the query string from this URL like below:

http://localhost:8084/blog1_1/title

Can you please suggest me how to do this?

Upvotes: 0

Views: 14692

Answers (4)

pvs
pvs

Reputation: 64

If you have your URL as an object and not a String you can do this:

Kotlin:

val url = URL("http://localhost:8084/blog1_1/title?uname=55%22")

val host = url.host
val path = url.path

val newUrl = URL("https://$host$path") 

Java:

URL url = new URL("http://localhost:8084/blog1_1/title?uname=55%22");
    
String host = url.getHost();
String path = url.getPath();

URL newUrl = new URL("https://" + host + path);

Upvotes: 0

Vladimir Eremeev
Vladimir Eremeev

Reputation: 372

Java:

String newURL = Uri.parse("YOUR_URL").getPath()

Kotlin:

val newURL = Uri.parse("YOUR_URL").path

Upvotes: 0

Brijesh Bhatt
Brijesh Bhatt

Reputation: 3830

String url="http://localhost:8084/blog1_1/title?uname=55%22";
String onlyUrl=url.substring(0,url.lastIndexOf("?")); //this has the URL

Upvotes: 5

vefthym
vefthym

Reputation: 7462

Assuming the url is a java String:

String newURL = url.substring(0, url.indexOf("?"));

should do the trick...

Upvotes: 4

Related Questions