路径加密 (路径加密)

 

思路:通过char和stringbuilder

// @Title: 路径加密 (路径加密)
// @Author: qisiii
// @Date: 2022-02-06 23:14:33
// @Runtime: 0 ms
// @Memory: 39.4 MB
// @comment: 通过char和stringbuilder
// @flag: GREEN
class Solution {
        public String replaceSpace(String s) {
        char[] chars = s.toCharArray();
        String rep="%20";
        StringBuilder str=new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (aChar==' '){
                str.append(rep);
            }else {
                str.append(aChar);
            }
        }
        return str.toString();
    }
}

思路:replace方法

// @Title: 路径加密 (路径加密)
// @Author: qisiii
// @Date: 2022-02-06 23:10:24
// @Runtime: 0 ms
// @Memory: 39.5 MB
// @comment: replace方法
// @flag: GREEN
class Solution {
    public String replaceSpace(String s) {
       s= s.replace(" ","%20");
       return s;
    }
}

思路:

// @Title: 路径加密 (路径加密)
// @Author: qisiii
// @Date: 2022-02-19 16:47:56
// @Runtime: 0 ms
// @Memory: 39.5 MB
// @comment: 
// @flag: 
class Solution {
    public String replaceSpace(String s) {
        return s.replace(" ", "%20");
    }
}

思路:

// @Title: 路径加密 (路径加密)
// @Author: qisiii
// @Date: 2022-02-19 16:51:48
// @Runtime: 0 ms
// @Memory: 39.4 MB
// @comment: 
// @flag: 
class Solution {
  public String replaceSpace(String s) {
        char[] c=s.toCharArray();
        StringBuilder str=new StringBuilder();
        for(int i=0;i<c.length;i++){
            if(c[i]==' '){
                str.append("%20");
            }else{
                str.append(c[i]);
            }
        }
        return str.toString();
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18