zoukankan      html  css  js  c++  java
  • P44、面试题4:替换空格

    题目:请实现一个函数,把字符串中的每个空格替换成“%20”。例如输入“We are happy.”,则输出“We%20are%20happy.”。

      如果用java string类中提供的replace方法可以很快的进行替换。

    代码实现:

    package com.yyq;
    
    /**
     * Created by Administrator on 2015/9/4.
     */
    public class ReplaceBlank {
        public static void main(String args[]){
            String str = "we are happy";
            String newStr = str.replace(" ","%20");
            System.out.println(newStr);
        }
    }

    如果是按照很原始的方法进行替换的话,里面包含着大技巧。

    我们可以先遍历一次字符串,这样就能统计出字符串中空格的总数,并可以由此计算出替换之后的字符串的总长度。每替换一个空格,长度增加2,因此替换以后字符串的长度等于原来长度加上2乘以空格数目。从字符串的后面开始复制和替换,直到都走到字符串首位。

    代码实现:

    package com.yyq;
    
    import java.util.Arrays;
    
    /**
     * Created by Administrator on 2015/9/4.
     */
    public class ReplaceBlank {
        private static int maxLength = 100;
    
        //length 为字符数组string的总容量,新增长的数组长度不能超过length
        public static void repalceBlank(String str, int trueLength, int maxLength) {
            if (str == null || maxLength <= 0) {
                return;
            }
        //如果是传统的静态数组,java中是不允许动态扩充的,如果需要扩充的话,则需要使用java自带的累积框架,如List
            char oldString[] = str.toCharArray();
            int numberBlank = 0;
            int i = 0;
            while (i < trueLength) {
                if (oldString[i] == ' ') {
                    numberBlank++;
                }
                i++;
            }
    
            int newLength = trueLength + numberBlank * 2;
            //新开辟一个数组
            char newString[] = new char[newLength];
            for (int j = 0; j < newLength; j++) {
                newString[j] = 0;
            }
            if (newLength > maxLength) {
                return;
            }
            int indexOld = trueLength - 1;
            int indexNew = newLength - 1;
            while (indexOld >= 0 && indexOld <= indexNew) {
                if (oldString[indexOld] == ' ') {
                    newString[indexNew] = '0';
                    indexNew--;
                    newString[indexNew] = '2';
                    indexNew--;
                    newString[indexNew] = '%';
                    indexNew--;
                } else {
                    newString[indexNew--] = oldString[indexOld];
                }
                indexOld--;
            }
            System.out.println(new String(newString));
        }
        public static void Test(String testName, String str) {
            if (str == null) return;
            if (testName != null) {
                System.out.println(testName + "======");
                repalceBlank(str, str.length(), maxLength);
            }
        }
    
    // 空格在句子中间
        public void Test1()
        {
            String str = "ab c";
            Test("Test1(空格在句子中间)", str);
        }
    
    // 空格在句子开头
        public void Test2()
        {
            String str = " helloworld";
            Test("Test2(空格在句子开头)", str);
        }
    
    
    // 空格在句子末尾
    
        public void Test3()
        {
            String str = "helloworld ";
            Test("Test3(空格在句子末尾)", str);
        }
    
    // 连续有两个空格
        public void Test4()
        {
            String str = "hello  world";
            Test("Test4(连续有两个空格)", str);
        }
    
    // 传入NULL
        public void Test5()
        {
            Test("Test5(传入null)", null);
        }
    
    // 传入内容为空的字符串
    
        public void Test6()
        {
            String str = "";
            Test("Test6传入内容为空的字符串", str);
        }
    
    
    //传入内容为一个空格的字符串
        public void Test7()
        {
            String str = " ";
            Test("Test7(传入内容为一个空格的字符串)", str);
        }
    
    
    // 传入的字符串没有空格
        public void Test8()
        {
            String str = "helloworld";
            Test("Test8(传入的字符串没有空格)", str);
        }
    
    
    // 传入的字符串全是空格
        public void Test9()
        {
            String str = "   ";
            Test("Test9(传入的字符串全是空格)", str);
        }
    
    
        public static void main(String[] args) {
    // TODO Auto-generated method stub
            ReplaceBlank test = new ReplaceBlank();
            test.Test1();
            test.Test2();
            test.Test3();
            test.Test4();
            test.Test5();
            test.Test6();
            test.Test7();
            test.Test8();
            test.Test9();
        }
    }

    输出结果:

    Test1(空格在句子中间)======

    ab%20c

    Test2(空格在句子开头)======

    %20helloworld

    Test3(空格在句子末尾)======

    helloworld%20

    Test4(连续有两个空格)======

    hello%20%20world

    Test6传入内容为空的字符串======

    Test7(传入内容为一个空格的字符串)======

    %20

    Test8(传入的字符串没有空格)======

    helloworld

    Test9(传入的字符串全是空格)======

    %20%20%20

  • 相关阅读:
    通知:逆天异常库 V1.0版本支持下载了~~
    【源码】Word转PDF V1.0.1 小软件,供新手参考
    GitHub实战系列汇总篇
    GitHub实战系列~4.把github里面的库克隆到指定目录+日常使用 2015-12-11
    GitHub实战系列~3.提交github的时候过滤某些文件 2015-12-10
    Windows无法安装到这个磁盘。请确保在计算机的BIOS菜单中启用了磁盘控制器
    GitHub实战系列~2.把本地项目提交到github中 2015-12-10
    Git异常:fatal: could not create work tree dir 'XXX': No such file or directory
    GitHub实战系列~1.环境部署+创建第一个文件 2015-12-9
    肉肉好走,愿你在异界依旧快乐活泼
  • 原文地址:https://www.cnblogs.com/yangyquin/p/4908260.html
Copyright © 2011-2022 走看看