标签:for iss creat 实现 替换空格 space ace [] 剑指offer
/**
* 题目描述 :
* 请实现一个函数,将一个字符串中的每个空格替换成“%20”.
* 例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy.
* Created by guo.chen on 2018/9/9.
*/
public class SolutionⅡ {
/**
*把字符串转为 char 数组,遍历数组替换空格为一个标志,再转为字符串
* 替换字符串中的标志
* @param str
* @return
*/
public static String replaceSpace(String str) {
char[] array = str.toCharArray();
for(int i=0;i<array.length;i++){
if(Character.isSpaceChar(array[i]))
array[i] = ‘,‘;
}
str = new String(array);
return str.replaceAll(",","%20");
}
public static void main(String[] args){
String str = new String(" We are Javaer ");
str = replaceSpace(str);
System.out.println(str);
}
}
标签:for iss creat 实现 替换空格 space ace [] 剑指offer
原文地址:https://www.cnblogs.com/zmngc/p/9613901.html