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

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

代码实现:

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

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

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

代码实现:

  1. package com.yyq;
  2.  
  3. import java.util.Arrays;
  4.  
  5. /**
  6. * Created by Administrator on 2015/9/4.
  7. */
  8. public class ReplaceBlank {
  9. private static int maxLength = 100;
  10.  
  11. //length 为字符数组string的总容量,新增长的数组长度不能超过length
  12. public static void repalceBlank(String str, int trueLength, int maxLength) {
  13. if (str == null || maxLength <= 0) {
  14. return;
  15. }
  16. //如果是传统的静态数组,java中是不允许动态扩充的,如果需要扩充的话,则需要使用java自带的累积框架,如List
  17. char oldString[] = str.toCharArray();
  18. int numberBlank = 0;
  19. int i = 0;
  20. while (i < trueLength) {
  21. if (oldString[i] == ' ') {
  22. numberBlank++;
  23. }
  24. i++;
  25. }
  26.  
  27. int newLength = trueLength + numberBlank * 2;
  28. //新开辟一个数组
  29. char newString[] = new char[newLength];
  30. for (int j = 0; j < newLength; j++) {
  31. newString[j] = 0;
  32. }
  33. if (newLength > maxLength) {
  34. return;
  35. }
  36. int indexOld = trueLength - 1;
  37. int indexNew = newLength - 1;
  38. while (indexOld >= 0 && indexOld <= indexNew) {
  39. if (oldString[indexOld] == ' ') {
  40. newString[indexNew] = '0';
  41. indexNew--;
  42. newString[indexNew] = '2';
  43. indexNew--;
  44. newString[indexNew] = '%';
  45. indexNew--;
  46. } else {
  47. newString[indexNew--] = oldString[indexOld];
  48. }
  49. indexOld--;
  50. }
  51. System.out.println(new String(newString));
  52. }
  53. public static void Test(String testName, String str) {
  54. if (str == null) return;
  55. if (testName != null) {
  56. System.out.println(testName + "======");
  57. repalceBlank(str, str.length(), maxLength);
  58. }
  59. }
  60.  
  61. // 空格在句子中间
  62. public void Test1()
  63. {
  64. String str = "ab c";
  65. Test("Test1(空格在句子中间)", str);
  66. }
  67.  
  68. // 空格在句子开头
  69. public void Test2()
  70. {
  71. String str = " helloworld";
  72. Test("Test2(空格在句子开头)", str);
  73. }
  74.  
  75. // 空格在句子末尾
  76.  
  77. public void Test3()
  78. {
  79. String str = "helloworld ";
  80. Test("Test3(空格在句子末尾)", str);
  81. }
  82.  
  83. // 连续有两个空格
  84. public void Test4()
  85. {
  86. String str = "hello world";
  87. Test("Test4(连续有两个空格)", str);
  88. }
  89.  
  90. // 传入NULL
  91. public void Test5()
  92. {
  93. Test("Test5(传入null)", null);
  94. }
  95.  
  96. // 传入内容为空的字符串
  97.  
  98. public void Test6()
  99. {
  100. String str = "";
  101. Test("Test6传入内容为空的字符串", str);
  102. }
  103.  
  104. //传入内容为一个空格的字符串
  105. public void Test7()
  106. {
  107. String str = " ";
  108. Test("Test7(传入内容为一个空格的字符串)", str);
  109. }
  110.  
  111. // 传入的字符串没有空格
  112. public void Test8()
  113. {
  114. String str = "helloworld";
  115. Test("Test8(传入的字符串没有空格)", str);
  116. }
  117.  
  118. // 传入的字符串全是空格
  119. public void Test9()
  120. {
  121. String str = " ";
  122. Test("Test9(传入的字符串全是空格)", str);
  123. }
  124.  
  125. public static void main(String[] args) {
  126. // TODO Auto-generated method stub
  127. ReplaceBlank test = new ReplaceBlank();
  128. test.Test1();
  129. test.Test2();
  130. test.Test3();
  131. test.Test4();
  132. test.Test5();
  133. test.Test6();
  134. test.Test7();
  135. test.Test8();
  136. test.Test9();
  137. }
  138. }

输出结果:

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

ab%20c

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

%20helloworld

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

helloworld%20

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

hello%20%20world

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

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

%20

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

helloworld

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

%20%20%20

P44、面试题4:替换空格的更多相关文章

  1. 【剑指offer】面试题 5. 替换空格

    面试题 5. 替换空格 题目:请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy. 则经过替换之后的字符串为We%20Are%20Hap ...

  2. 剑指Offer:面试题4——替换空格(java实现)

    问题描述:请实现一个函数,把字符串中的每个空格替换成"%20". 例如: 输入:"We are happy." 输出:"We%20are%20happ ...

  3. C++版 - 剑指offer 面试题4: 替换空格 题解

    面试题4:替换空格 提交网址: http://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=1 ...

  4. 剑指offer编程题Java实现——面试题4替换空格

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

  5. 剑指offer-面试题4.替换空格

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

  6. 剑指offer面试题4 替换空格(java)

    注:利用java中stringBuilder,append,length方法很方便的解决字符串问题 /* * 剑指offer 替换空格 * xsf * */ /*开始替换空格的函数,length为原数 ...

  7. 剑指offer面试题4 替换空格(c)

  8. 面试题04_替换空格_剑指Offer系列

    题目描写叙述 请实现一个函数,将一个字符串中的空格替换成"%20". 比如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 解题思路 ...

  9. 《剑指offer》面试题4 替换空格 Java版

    (给一个足够长的字符数组,其中有一段字符,将' '(空格)替换成'%' '2' '0'三个字符,原字符段由'\0'结尾) 书中方法:这道题如果从头到尾扫描数组并替换,会涉及到数组的移动.如果不移动元素 ...

  10. 【剑指Offer】面试题05.替换空格

    题目 请实现一个函数,把字符串 s 中的每个空格替换成"%20". 示例 1: 输入:s = "We are happy." 输出:"We%20are ...

随机推荐

  1. Z-Stack ZMain学习

    [注:本文源自博客园http://www.cnblogs.com/cherishui/,为尊重劳动者成果,如需转载请保留此行] 在TI已有的Z-Stack的工程下面,打开已有的demo文件,通过分析不 ...

  2. L005-oldboy-mysql-dba-lesson05

    L005-oldboy-mysql-dba-lesson05 在线改表工具:pt-onine-schema-change 来自为知笔记(Wiz)

  3. pdf转chm的实现方法

    相比pdf, CHM电子书在Windows系统下不需要安装额外的浏览器即可进行阅读,其内容是基于浏览器的风格,更容易被用户所接受.而且, 具有更强大的功能配置,比如可提供强大的全文搜索.索引.书签等的 ...

  4. 选中excel中的对象

    2007在查找和选择中点击“选择对象”,然后再全选全个sheet(ctrl+a)就可以看到了. 2010 “选择对象”在  开始——查找和选择——选择对象

  5. GridView ItemCommand

    GridView ItemCommand中取某行某列的值方法,这里提供两个常用的: 一.用CommandArgument属性取值页面如下: <asp:TemplateColumn HeaderT ...

  6. 【Spring-boot多数据库】Spring-boot JDBC with multiple DataSources sample

    application.properties spring.ds_items.driverClassName=org.postgresql.Driver spring.ds_items.url=jdb ...

  7. Entity Framework 学习笔记(一)安装

    1.通过 VS2013 下载,这个没有限制,因为我用的vs是2013 Entity Framework包

  8. 微软职位内部推荐-Senior NLP Scientist & Developer

    微软近期Open的职位: Contact Person: Winnie Wei (wiwe@microsoft.com )Senior Software Development Engineer/NL ...

  9. custom activities

    Useful Sharepoint Designer Custom Workflow Activities http://spdactivities.codeplex.com/ http://stac ...

  10. linq and rest api in sharepoint

    //1.make sure your application using the .net fromwork 3.5 //2.create entity classes using the instr ...