class Solution {
public String replaceSpace(String s) {
int length = s.length();
char[] array = new char[length * 3];
int size = 0;
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
if (c == ' ') {
array[size++] = '%';
array[size++] = '2';
array[size++] = '0';
} else {
array[size++] = c;
}
}
String newStr = new String(array, 0, size);
return newStr;
}
}
public class ReplaceSpace {
public static String replaceSpace(StringBuffer str) {
if(str == null){
return null;
}
StringBuilder res = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 0x20){
res.append("%20");
}else{
res.append(str.charAt(i));
}
}
return res.toString();
}
public static void main(String[] args) {
System.out.println(replaceSpace(new StringBuffer("We are happy")));
}
}
剑指 Offer 05. 替换空格
评论