描述
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
示例1
输入:
“We Are Happy”
复制
返回值:
“We%20Are%20Happy”
代码
public class JZ2_replaceSpace {
public static String replaceSpace(String s) {
StringBuilder sb = new StringBuilder();
String[] arrStr = s.split("");
for (String str : arrStr) {
if (str.equals(" ")) {
sb.append("%20");
}else {
sb.append(str);
}
}
return sb.toString();
}
public static void main(String[] args) {
String res = replaceSpace("We Are Happy");
System.out.println(res);
}
}