需要用百分号Java替换字符串中的空格

我需要用%符号替换字符串中的空格,但我遇到了一些问题,我尝试的是:

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

但是它在替换function中给了我一个错误。

然后:

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

但它仍然在替换function中给我一个错误。

我尝试使用unicode符号:

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

但它仍然给出错误。

有一个简单的方法吗?

String.replace(String, String)是您想要的方法。

更换

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

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

我建议你在java中使用URL Encoder来编码Strings

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

我曾经遇到某些框架,遇到过这样的问题。 我没有足够的代码可以肯定地知道,但可能发生的是你正在使用的任何http框架,在我的情况下它是spring,再次编码URL。 我花了几天时间试图解决类似的问题,我认为字符串替换和URI.builder()被破坏了。 最终出现问题的是我的http框架已经采用了我编码的url,并再次编码。 这意味着它看到“%20”的任何地方,它会看到’%’的值,并将其切换为’%’http码,“%25”,从而产生。“%2520”。 然后请求将失败,因为%2520没有转换为我的服务器所期望的空间。 虽然问题是我的编码之一无法正常工作,但这确实是一个编码太多次的问题。 我在下面的一个项目中有一些工作代码的例子

 //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); 

可能有一种更优雅的方式来保持url编码两次。 我希望这个例子可以帮助您找到正确的方向或消除可能性。 祝好运。

试试这个:

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

不应该替换空格,试试这个

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