原文:
Write a method to replace all spaces in a string with ‘%20’.
译文:
写一个函数,把字符串中所有的空格替换为%20 。
public static void main(String args[])
{
String s = "i love u.";
StringBuffer buffer = new StringBuffer();
for (int i = 0, len = s.length(); i < len; ++i)
{
if (s.charAt(i) != ' ')
buffer.append(s.charAt(i));
else
buffer.append("%20");
}
String ans = buffer.toString();
System.out.println(ans);
}