Swap whitespace for %20 in Java

Asked

Viewed 740 times

6

I have the following URL:

String Url = "www.minhaUrl/pagina/meus parametros tem espaços em branco";

I need to turn it into:

String Url = "www.minhaUrl/pagina/meus%20parametros%20tem%20espaços%20em%branco";

2 answers

14


Use the method

URLEncoder.encode("meus parametros tem espaços em branco", "UTF-8");

The Urlencoder is the best way to do this since you are messing with a URL and it will handle any other case necessary.

  • 4

    This is the correct way to treat URL’s, +1

4

Url = Url.replaceAll(" ", "%20");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.