Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)

Can anyone help me how I can add the URL when the user add any query like:

The URL is:

http://example.com/query?q=

And if some user entered some query:

random word £500 bank $

So the result:

http://example.com/query?q=random%20word%20%A3500%20bank%20%24

Can anyone help me with this?

1 Answer

0 votes
by (26.7k points)

You can use URLEncoder to do that, you can use UTF-8 if you are using an older version lower than 10, and if you are using a Java version of 11 or newer, you can use StandardCharsets.UTF_8.toString()

String q = "random word £500 bank $";

String url = "https://example.com?q=" + URLEncoder.encode(q, StandardCharsets.UTF_8);

I hope this will help.

Want to know more about Java? Prefer this tutorial on Learn Java.

Want to become a Java Expert? Join Java Certification now!!

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...