我需要用 % 符号替换字符串内的空格,但我遇到了一些问题,我尝试的是:
imageUrl = imageUrl.replace(' ', "%20");
但它在替换功能中给了我一个错误。
Then:
imageUrl = imageUrl.replace(' ', "%%20");
但它仍然在替换功能中给我一个错误。
我尝试使用 unicode 符号:
imageUrl = imageUrl.replace(' ', (char) U+0025 + "20");
但它仍然给出错误。
有简单的方法吗?
String.replace(String, String)
是你想要的方法。
replace
imageUrl.replace(' ', "%");
with
imageUrl.replace(" ", "%");
System.out.println("This is working".replace(" ", "%"));
我建议你使用URL Encoder
用于编码Strings
在Java中。
String searchQuery = "list of banks in the world";
String url = "http://mypage.com/pages?q=" + URLEncoder.encode(searchQuery, "UTF-8");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)