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!!