替换空格

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

/**
   通不过的代码,当happy后有多个空格,split没有产生拆分
**/
public class Solution {
    public String replaceSpace(StringBuffer str) {
        if(str==null) return null;
        String temp=str.toString();
        String[] arr=temp.split(" ");
        StringBuffer result=new StringBuffer();
        boolean isFirst=true;
        for(String s : arr){
            if(isFirst){
                result.append(s);
                isFirst=false;
            }else{
                result.append("%20");
                result.append(s);
            }
        }
        return result.toString();
    }
}

实现一:

public class Solution {
    public String replaceSpace(StringBuffer str) {
        if(str==null) return null;
        return str.toString().replaceAll(" ","%20");
//return str.toString().replace(" ","%20"); } }

public String replace(char oldChar,char newChar)
返回一个新字符串,通过用 newChar 替换此字符串中出现的所有 oldChar 。 如 果 oldChar 在此 String 对象表示的字符序列中没有出现,则返回对此 String 对象的引用。否则,创建一个新的 String 对象,用来表示与此 String 对象表示的字符序列相等的字符序列,除每个出现的 oldChar 都被一个 newChar 替换之外。

public String replaceAll(String regex,String replacement)

使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的每个子字符串。此方法调用的 str.replaceAll(regex, repl) 形式产生与以下表达式完全相同的结果:
Pattern.compile(regex).matcher(str).replaceAll(repl)
参数:
  regex - 用来匹配此字符串的正则表达式 
  返回:得到的 String 
  抛出: PatternSyntaxException - 如果正则表达式的语法无效。

实现二

public class Solution {
    public String replaceSpace(StringBuffer str) {
         if(str==null)
            return null;
        String s = str.toString();
         char []ss=s.toCharArray();
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<ss.length;i++)
            {
            if(ss[i]==' ')
            {
                 sb.append("%20");
           }
           else
               sb.append(ss[i]);
        }
        return sb.toString();
    }
}

实现三

public class Solution {
    public String replaceSpace(StringBuffer str) {
        int spacenum = 0;
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)==' ')
                spacenum++;
        }
        int indexold = str.length()-1; 
        int newlength = str.length() + spacenum*2;
        int indexnew = newlength-1;
        str.setLength(newlength);
        for(;indexold>=0 && indexold<newlength;--indexold){ 
                if(str.charAt(indexold) == ' '){  //
                str.setCharAt(indexnew--, '0');
                str.setCharAt(indexnew--, '2');
                str.setCharAt(indexnew--, '%');
                }else{
                    str.setCharAt(indexnew--, str.charAt(indexold));
                }
        }
        return str.toString();
    }
}

注:StringBuffer

length()和capacity()

通过调用length()方法可以得到当前StringBuffer的长度。而通过调用capacity()方法可以得到总的分配
StringBuffer sb = new StringBuffer("Hello");
 
 System.out.println("buffer = "+sb);  //Hello
 System.out.println("length = "+sb.length);  //5
 System.out.println("capacity = "+sb.capacity);  //21
 /**
     * Constructs a string buffer initialized to the contents of the
     * specified string. The initial capacity of the string buffer is
     * {@code 16} plus the length of the string argument.
     *
     * @param   str   the initial contents of the buffer.
     */
    public StringBuffer(String str) {
        super(str.length() + 16);
        append(str);
    }

 

posted on 2019-01-20 12:21  溪水静幽  阅读(94)  评论(0)    收藏  举报