一、题目
请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入“We are happy.”,则输出“We%20are%20happy.”。
二、思路
在字符串尾部填充任意字符,使得字符串的长度等于替换之后的长度。因为一个空格要替换成三个字符(%20),因此当遍历到一个空格时,需要在尾部填充两个任意字符。
令 P1 指向字符串原来的末尾位置,P2 指向字符串现在的末尾位置。P1 和 P2 从后向前遍历,当 P1 遍历到一个空格时,就需要令 P2 指向的位置依次填充 02%(注意是逆序的),否则就填充上 P1 指向字符的值。
从后向前遍是为了在改变 P2 所指向的内容时,不会影响到 P1 遍历原来字符串的内容。
三、解决问题
3.1 代码实现
- 调用自带函数
/**
* 调用自带函数
* @param str
* @return
*/
public String replaceSpace1(StringBuffer str) {
if(null == str || str.length() == 0){
System.out.println("字符串输入无效!");
return null;
}
//replace(char oldChar, char newChar)
//返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
return str.toString().replace(" ", "%20");
}
- 用新的数组存
public String replaceSpace(StringBuffer str){
if(null == str || str.length() <= 0){
System.out.println("字符串输入无效!");
return null;
}
int p1 = str.length() - 1;
for (int i = 0; i < p1; i++) {
//返回 char指定索引处的值。
if (str.charAt(i) == ' '){
str.append(" ");
}
}
int p2 = str.length() -1;
while (p1 >= 0 && p2 > p1){
char c = str.charAt(p1--);
if (c == ' '){
//指定索引处的字符设置为 ch 。
str.setCharAt(p2--,'0');
str.setCharAt(p2--,'2');
str.setCharAt(p2--,'%');
}else {
str.setCharAt(p2--,c);
}
}
return str.toString();
}
3.调用String自带方法
/**
* 直接方法
*/
public String replaceSpace2(StringBuffer str) {
if (null == str){
return null;
}
int fromIndex = 0;
int index = 0;
StringBuffer temp = new StringBuffer();
while(index <= str.length()){
//返回指定子串的第一次出现的字符串中的索引,从指定的索引开始。
//从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引
//fromIndex - 搜索开始处的索引
index = str.indexOf(" ",fromIndex);
if (index >= 0){
//返回一个字符序列,该序列是该序列的子序列。
temp.append(str.subSequence(fromIndex,index)).append("%20");
index = index + 1;
fromIndex = index;
}else{
//返回一个新的字符序列,该字符序列是此序列的子序列
//beginIndex -- 起始索引(包括)。
//endIndex -- 结束索引(不包括)。
temp.append(str.subSequence(fromIndex,str.length()));
break;
}
}
return temp.toString();
}
3.2 单元测试
测试用例
1.字符串中无空格
2.字符串中含有空格(连续空格,空格在首尾等)
3.字符串为空字符串或者为null
package SwordOffer;
/**
* @author kankan
* @creater 2019-10-29 8:54
*/
// 题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入“We are happy.”,
// 则输出“We%20are%20happy.”。
public class Solution4 {
/**
* 双指针法
* @param str
* @return
*/
public String replaceSpace(StringBuffer str){
if(null == str || str.length() == 0){
System.out.println("字符串输入无效!");
return null;
}
int p1 = str.length() - 1;
for (int i = 0; i < p1; i++) {
//返回 char指定索引处的值。
if (str.charAt(i) == ' '){
str.append(" ");
}
}
int p2 = str.length() -1;
while (p1 >= 0 && p2 > p1){
char c = str.charAt(p1--);
if (c == ' '){
//指定索引处的字符设置为 ch 。
str.setCharAt(p2--,'0');
str.setCharAt(p2--,'2');
str.setCharAt(p2--,'%');
}else {
str.setCharAt(p2--,c);
}
}
return str.toString();
}
/**
* 调用自带函数
* @param str
* @return
*/
public String replaceSpace1(StringBuffer str) {
if(null == str || str.length() == 0){
System.out.println("字符串输入无效!");
return null;
}
//replace(char oldChar, char newChar)
//返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
return str.toString().replace(" ", "%20");
}
/**
* 输入为null
*/
public void test1() {
System.out.print("Test1:");
StringBuffer sBuffer = null;
String s = replaceSpace(sBuffer);
System.out.println(s);
}
/**
* 输入为空字符串
*/
public void test2() {
System.out.print("Test2:");
StringBuffer sBuffer = new StringBuffer("");
String s = replaceSpace(sBuffer);
System.out.println(s);
}
/**
* 输入字符串无空格
*/
public void test3() {
System.out.print("Test3:");
StringBuffer sBuffer = new StringBuffer("abc");
String s = replaceSpace(sBuffer);
System.out.println(s);
}
/**
* 输入字符串为首尾空格,中间连续空格
*/
public void test4() {
System.out.print("Test4:");
StringBuffer sBuffer = new StringBuffer("We Are Happy");
//String s = replaceSpace(sBuffer);
String s = replaceSpace1(sBuffer);
System.out.println(s);
}
public static void main(String[] args) {
Solution4 rs = new Solution4();
rs.test1();
rs.test2();
rs.test3();
rs.test4();
}
}