91821-ReplaceSpace

package valley.day07;

public class BlankReplace {

    public static void main(String[] args) {
        String s = "We are happy.";
        BlankReplace test = new BlankReplace();
        String re = test.replaceSpace(s);
        System.out.println(re);
        //*********************************
        char re1 = test.replaceSpace1(s);
        System.out.println(re1);


    }

    public String replaceSpace(String s) {
        //使用StringBuilder去拼接
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            if (" ".equals(String.valueOf(c))) {
                sb.append("%20");
            } else {
                sb.append(s.charAt(i));
            }
        }
        return sb.toString();
    }

    //To loop every char
    public char replaceSpace1(String s) {
        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            System.out.print(c);
            System.out.print("|");
        }
        return ' ';
    }
}

上一篇:jsp获取当前目录下的文件和目录,获取windows盘符


下一篇:C# 计算时间间隔,两个时间差(年月日时分秒)