题目:请实现一个函数,把字符串中的每个空格替换成“%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

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. [CSS]下拉菜单

    原理:先让下拉菜单隐藏,鼠标移到的时候在显示出来 1>display 无动画效果,图片是秒出 2>opacity 有动画效果,我这里是1S出现,推荐配合绝对定位使用

  2. C 语言循环之break、continue

    在C 编程的过程中,我们很多时候都会用到循环,但有时需要中途跳出整个循环,或跳过某一次循环,这时就需要用到break或continue,关于二者的使用很多书籍和博文都有很相近的说明,此处不做任何讲解, ...

  3. Scrapy简介

    什么是Scrapy? Scrapy是一个快速.高级的爬行器和网页抓取框架,用来抓取网站和提取网页中结构化的数据.它被广泛的使用于监控数据采集和自动化测试. 参考:http://scrapy.org/

  4. xaml中绑定单例属性

    在项目中经常会遇到,同一个字典表绑定到多个ItemsControl上的情况,可以在单例中创建一个List,xaml上绑定即可.看代码: 1,XAML <Grid> <StackPan ...

  5. Mooncake (排序+贪心)

    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...

  6. Ubuntu 14.04的SWAP 为0

    [@@@@@@]# free total used free shared buffers cached Mem: 1024976 248992 775984 0 16820 73128 -/+ bu ...

  7. (转)Qt Model/View 学习笔记 (六)——在views中选择数据项

    在views中选择数据项 概念 用于新的view类中的选择模型比Qt3中的模型有了很大的改进.它为基于model/view架构的选择提供了更为全面的描述.尽管对提供了的views来说,负责操纵选择的标 ...

  8. GridView控件RowDataBound事件中获取列字段值的几种途径

    前台: <asp:TemplateField HeaderText="充值总额|账号余额"> <ItemTemplate> <asp:Label ID ...

  9. strcmp的源码实现

    微软方法: int __cdecl strcmp (const char *src, const char *dst) { ; while(!(ret = *(unsigned char *)src ...

  10. To get TaskID's Integer ID value from the GUID in SharePoint workflow

    list.GetItemByUniqueId(guid).ID int itemID = spList.Items[new Guid("")].ID;