package com.zmz.algorithm.string;

/**
 * @author 张明泽
 * Create by 2022/5/26 11:41
 */
public class ReplaceSpace {
    public static void main(String[] args) {
        String str = "We are happy.";
        String result = replace2(str);
        System.out.println(result);
    }
    public static String replace(String str) {
        StringBuffer stringBuffer = new StringBuffer();
        for (char c : str.toCharArray()) {
            if (c == ' ') {
                stringBuffer.append("%20");
            } else {
                stringBuffer.append(c);
            }
        }
        return stringBuffer.toString();
    }
    /**
     * 调用函数
     */
    public static String replace2(String str) {
        return str.replace(" ","%20");
    }
}
最后修改:2022 年 05 月 27 日 07 : 10 AM
赏杯咖啡喝 谢谢您~