Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 707 Bytes

替换空格.md

File metadata and controls

33 lines (24 loc) · 707 Bytes

####替换空格

请实现一个函数,把字符串中的每个空格替换成%20。

package lintcode;

public class ReplaceBlank {
    public static String replaceString(String str){
        StringBuilder sb = new StringBuilder();

    for (int i = 0; i < str.length(); i++){
        if (str.charAt(i) == ' '){
            sb.append("%20");
        }else{
            sb.append(str.charAt(i));
        }
    }

    return sb.toString();
}

public static void main(String[] args) {
    String str = "we are happy";

    String ret = replaceString(str);

    System.out.println(ret);
}

}ing(str);

    System.out.println(ret);
}

}