您的位置:首页 > 编程语言 > Java开发

替换字符串中的空格

2017-12-07 14:39 239 查看

需求:

  将字符串中的空格转成"%20",然后返回

分析:

  在java中,字符串的字符替换有三种方式:replace(char oldchar, char newchar) replace(CharSequnce old, charSequence new) replaceAll(String old, String new)。

  1、判断字符串是否是null或者长度是0,这两种情况下不需要对字符串进行遍历,直接返回相应的结果即可

  2、遍历字符串,得到空格的个数count,如果count=0,说明字符串中没有空格,直接返回原始字符串即可

  3、创建字符数组,用于存储替换后的字符串的所有字符,字符数组的长度是(原始字符串长度+空格个数×2)

  4、遍历字符串,如果不是空格,直接添加该字符到数组中,如果是空格,添加三个字符到数组中,注意,遍历字符串和字符数组的角标需要用两个不同的变量表示

  如果传入的参数是字符数组,并且长度足够长,那么就可以在原地进行修改。遍历字符串,求出其中空格的数量,那么新的字符串的长度就是原始字符串长度加上2乘以空格的数量。创建两个指针,分别指向原始字符串的结尾和新字符串的结尾,从后向前遍历原始字符串,如果不是空格,那么直接赋值,如果是空格,那么新的字符串的赋值%20,直到两个指针指向同一位置,说明所有的空格已经替换完毕,返回新字符串的长度.一般要求在原地进行修改的题目,都是保证有足够空间的。类似的题目包括合并两个排序数组,一般来说,可以创建新的数组,然后重新赋值,但是如果数组长度足够长,那么就可以在原地进行修改。

代码:

import java.util.*;

class ReplaceSpace{
//替换字符串中的空格
public static String replaceSpace(String str){
/*
//直接使用java中的函数即可实现此功能
return str.replace(" ", "%20");
*/
//判断该字符串是否是null,如果是,直接返回null
if(str == null){
return null;
}
if(str.length() == 0){
return str;
}

//查看该字符串中空格的个数,以便于创建大小合适的字符数组
int count = 0;//初始化空格个数0
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) == ' '){
count++;
}
}

//如果count=0,代表该字符串没有空格,直接返回原始字符串即可
if(count == 0){
return str;
}

//创建大小是str.length()+count*2的字符数组,即为新数组的长度
char[] arr = new char[str.length()+count*2];
int j = 0;//新数组的角标
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) != ' '){
arr[j++] = str.charAt(i);
}
else{
arr[j++] = '%';
arr[j++] = '2';
arr[j++] = '0';
}
}

//将字符数组转成字符串返回
return new String(arr);
}

public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String str;

while(scan.hasNext()){
str = scan.nextLine();

System.out.println(replaceSpace(str));
}
}
}

public class Solution {
/*
* @param string: An array of Char
* @param length: The true length of the string
* @return: The true length of new string
*/
public int replaceBlank(char[] string, int length) {
// write your code here
//如果string=null,返回0
if(string == null){
return 0;
}

//求空格数量
int numOfBlank = 0;
for(int i = 0; i < length; i++){
if(string[i] == ' '){
numOfBlank++;
}
}

//求新字符串的长度
int newLength = length+2*numOfBlank;

//从后向前遍历,更新字符数组
for(int i = length-1, j = newLength-1; i>=0 && j > i;){
if(string[i] == ' '){
string[j--] = '0';
string[j--] = '2';
string[j--] = '%';
i--;
}
else{
string[j--] = string[i--];
}
}

return newLength;
}
}

合并两个排序数组A和B,假设数组A长度可以容纳二者的合并结果。代码如下,都是从后向前遍历,即从没有分配值的空间开始分配值。

public class Solution {
/*
* @param A: sorted integer array A which has m elements, but size of A is m+n
* @param m: An integer
* @param B: sorted integer array B which has n elements
* @param n: An integer
* @return: nothing
*/
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
// write your code here
//如果m,n是负数,那么抛出异常
if(m < 0 || n < 0){
throw new IllegalArgumentException("invalid parameters");
}
//如果m=0,那么直接让A=B即可,如果n=0,那么直接返回
if(m == 0){
for(int i = 0; i < n; i++){
A[i] = B[i];
}
return;
}
if(n == 0){
return;
}

//从后向前遍历A,B数组,把较大值放到A的末尾
int i = m-1, j = n-1, k = m+n-1;
for(; i >= 0 && j >= 0;){
if(A[i] > B[j]){
A[k--] = A[i--];
}
else{
A[k--] = B[j--];
}
}

//如果A遍历完了
if(i < 0){
for(int t = j; t >= 0; t--){
A[k--] = B[t];
}
}
else{
//B遍历完了
return;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 字符串