请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

public class Solution {
    public String replaceSpace(StringBuffer str) {
        if(str==null)
        {
            return null;
        }
        StringBuilder newstr=new StringBuilder();
        for(int i=0;i<str.length();i++)
        {
            if(str.charAt(i)==' ')
            {
                newstr.append('%');
                newstr.append('2');
                newstr.append('0');
            }
            else
                newstr.append(str.charAt(i));
        }
        return newstr.toString();
    }
}

 

posted on 2020-03-15 17:33  *萌哈哈  阅读(523)  评论(0编辑  收藏  举报

导航