需要用百分号Java替换字符串中的空格 [英] Need to replace spaces inside string with percentual symbol Java
本文介绍了需要用百分号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.
然后:
imageUrl = imageUrl.replace(' ', "%%20");
但它仍然在替换函数中给我一个错误。
But It still gives me an error in the replace function.
我尝试使用unicode符号:
The I tried with the unicode symbol:
imageUrl = imageUrl.replace(' ', (char) U+0025 + "20");
但它仍然会出错。
有没有简单的方法呢?
推荐答案
String.replace (字符串,字符串)
是您想要的方法。
替换
imageUrl.replace(' ', "%");
imageUrl.replace(" ", "%");
System.out.println("This is working".replace(" ", "%"));
我建议您使用 URL编码器
用于编码字符串
在java中。
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");
这篇关于需要用百分号Java替换字符串中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文