Here is a java program to replace space with "%20" in a string. For example, the string "We Are Happy" will be changed to "We%20Are%20Happy".
Note that a space is a character while "%20" is a string.
//Replace Space With "%20" In A String
public class Solution {
public String replaceSpace(StringBuffer str) {
StringBuffer s = new StringBuffer();
for(int i=0;i<str.length();++i) {
if (str.charAt(i)==' ') {
s.append("%20");
} else {
s.append(str.charAt(i));
}
}
return s.toString();
}
}