LS_
LS_

Reputation: 7129

Need to replace spaces inside string with percentual symbol Java

I need to replace the spaces inside a string with the % symbol but I'm having some issues, what I tried is:

imageUrl = imageUrl.replace(' ', "%20");

But It gives me an error in the replace function.

Then:

imageUrl = imageUrl.replace(' ', "%%20");

But It still gives me an error in the replace function.

The I tried with the unicode symbol:

imageUrl = imageUrl.replace(' ', (char) U+0025 + "20");

But it still gives error.

Is there an easy way to do it?

Upvotes: 0

Views: 2383

Answers (4)

wolfaviators
wolfaviators

Reputation: 503

I've ran into issues like this in the past with certain frameworks. I don't have enough of your code to know for sure, but what might be happening is whatever http framework you are using, in my case it was spring, is encoding the URL again. I spent a few days trying to solve a similar problem where I thought that string replace and the URI.builder() was broken. What ended up being the problem was that my http framework had taken my encoded url, and encoded it again. that means that any place it saw a "%20", it would see the '%' charictor and switch it out for '%' http code, "%25", resulting in. "%2520". The request would then fail because %2520 didn't translate into the space my server was expecting. While the issue apeared to be one of my encoding not working, it was really an issue of encoding too many times. I have an example from some working code in one of my projects below

//the Url of the server
String fullUrl = "http://myapiserver.com/path/";

//The parameter to append. contains a space that will need to be encoded
String param 1 = "parameter 1"

//Use Uri.Builder to append parameter
Uri.Builder uriBuilder = Uri.parse(fullUrl).buildUpon();
uriBuilder.appendQueryParameter("parameter1",param1);

/* Below is where it is important to understand how your
http framework handles unencoded url. In my case, which is Spring 
framework, the urls are encoded when performing requests. 
The result is that a url that is already encoded will be 
encoded twice. For instance, if you're url is

     "http://myapiserver.com/path?parameter1=param 1"

and it needs to be read by the server as 
     "http://myapiserver.com/path?parameter1=param%201"

it makes sense to encode the url using URI.builder().append, or any valid
solutions listed in other posts. However, If the framework is already 
encoding your url, then it is likely to run into the issue where you
accidently encode the url twice: Once when you are preparing the URL to be 
sent, and once again when you are sending the message through the framework.

this results in sending a url that looks like
"http://myapiserver.com/path?parameter1=param%25201"
where the '%' in "%20" was replaced with "%25", http's representation of '%' 
when what you wanted was 
"http://myapiserver.com/path?parameter1=param%201"

this can be a difficult bug to squash because you can copy the url in the 
debugger prior to it being sent and paste it into a tool like fiddler and 
have the fiddler request work but the program request fail. 

since my http framework was already encoding the urls, I had to unencode the
 urls after appending the parameters so they would only be encoded once.
I'm not saying it's the most gracefull solution, but the code works.
*/
        String finalUrl = uriBuilder.build().toString().replace("%2F","/")
 .replace("%3A", ":").replace("%20", " ");

            //Call the server and ask for the menu. the Menu is saved to a    string
           //rest.GET() uses spring framework. The url is encoded again as 
part of the framework.
            menuStringFromIoms = rest.GET(finalUrl);

There is likely a more graceful way to keep a url from encoding twice. I hope this example helps point you on the right direction or eliminate a possability. Good luck.

Upvotes: 1

HungPV
HungPV

Reputation: 489

Replace spaces is not enought, try this

url = java.net.URLEncoder.encode(url, "UTF-8");

Upvotes: 0

Uma Kanth
Uma Kanth

Reputation: 5629

String.replace(String, String) is the method you want.

replace

imageUrl.replace(' ', "%");

with

imageUrl.replace(" ", "%");

System.out.println("This is working".replace(" ", "%"));

I suggest you to use a URL Encoder for Encoding Strings in java.

String searchQuery = "list of banks in the world";
String url = "http://mypage.com/pages?q=" + URLEncoder.encode(searchQuery, "UTF-8");

Upvotes: 6

Kinnar Vasa
Kinnar Vasa

Reputation: 407

Try this:

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

Upvotes: 1

Related Questions