我需要用 % 符号替换字符串内的空格,但我遇到了一些问题,我尝试的是:
imageUrl = imageUrl.replace(' ', "%20");
但是它在替换函数中给了我一个错误。
然后:
imageUrl = imageUrl.replace(' ', "%%20");
但它仍然在替换函数中给我一个错误。
我尝试使用 unicode 符号:
imageUrl = imageUrl.replace(' ', (char) U+0025 + "20");
但是还是报错。
有简单的方法吗?
最佳答案
String.replace(String, String)
is the method you want.
替换
imageUrl.replace(' ', "%");
与
imageUrl.replace(" ", "%");
System.out.println("This is working".replace(" ", "%"));
我建议您使用URL Encoder
在java中编码Strings
。
String searchQuery = "list of banks in the world";
String url = "http://mypage.com/pages?q=" + URLEncoder.encode(searchQuery, "UTF-8");
关于java - 需要用百分比符号Java替换字符串内的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31408187/