美文网首页
字符串替换

字符串替换

作者: 神奇的小蘑菇 | 来源:发表于2018-03-02 16:45 被阅读25次
    
    public static String replaceSpace(String input){
            char[] inputs = input.toCharArray();
            char[] containers = new char[100];
            int originLength = inputs.length;
            int spaceLength = 0;
            int i = 0;
    
            while(i < originLength) {
                if(' ' == inputs[i]) {
                    spaceLength ++;
                }
                containers[i] = inputs[i];
                i ++;
            }
            int changedLength = originLength + spaceLength * 2;
            int outputLength = originLength + spaceLength * 2;
            while(originLength > 0 && originLength < changedLength) {
                if(containers[originLength - 1] == ' '){
                    containers[-- changedLength] = '+';
                    containers[-- changedLength] = '+';
                    containers[-- changedLength] = '+';
                } else {
                    containers[--changedLength] = containers[originLength - 1];
                }
                originLength --;
            }
            String result = "";
            int j = 0;
            while (outputLength > 0) {
                String index = String.valueOf(containers[j]);
                result = result.concat(index);
                outputLength --;
                j++;
            }
            return result;
        }
    

    相关文章

      网友评论

          本文标题:字符串替换

          本文链接:https://www.haomeiwen.com/subject/nlgaxftx.html