Swap blanks for% 20 in Java

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";
    
asked by anonymous 15.03.2017 / 18:07

2 answers

14

Use the

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

URLEncoder is the best way to do this already that you're messing with a URL and it will handle any other case you need.

    
15.03.2017 / 18:14
4
Url = Url.replaceAll(" ", "%20");
    
15.03.2017 / 18:08