空格替换

设计一种方法,将一个字符串中的所有空格替换成 %20 。你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度。

你的程序还需要返回被替换后的字符串的长度。

对于字符串"Mr John Smith", 长度为 13

替换空格之后,参数中的字符串需要变为"Mr%20John%20Smith",并且把新长度 17 作为结果返回。

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
        for (int i = 0; i < length; i++){
            if (string[i] == ' '){
                for (int j = length + 2; j > i + 2; j--){//整个数组后移
                    string[j] = string[j - 2];
                }
                    string[i] = '%';
                    string[i + 1] = '2';
                    string[i + 2] = '0';
                    length = length + 2;
            }
        }
        return length;
    }
}
在编程中,字符串中的空格替换或删除是常见的需求,尤其是在处理输入数据或格式化输出时。以下是几种实现方式,涵盖 Java 和 C++ 语言,以及一些通用的思路。 ### 使用 Java 替换空格 在 Java 中,可以使用内置的 `String` 方法来替换空格。例如,使用 `replaceAll()` 方法,可以将字符串中的所有空格替换为其他字符或字符串: ```java public class ReplaceSpace { public static void main(String[] args) { String input = "Hello Google"; String output = input.replaceAll(" ", " AND "); System.out.println(output); // 输出: Hello AND Google } } ``` 如果需要更复杂的替换逻辑,例如逐字符处理,可以手动实现,而不是依赖内置的 API。这种情况下,可以遍历字符数组,统计空格数量,然后构建新的字符数组进行替换: ```java public class ReplaceSpaceManually { public static String replaceSpace(String input) { char[] originChars = input.toCharArray(); int spaceNum = 0; for (char c : originChars) { if (c == ' ') { spaceNum++; } } char[] newChars = new char[originChars.length + 2 * spaceNum]; // 每个空格替换成3个字符 int newStrIndex = 0; for (char c : originChars) { if (c != ' ') { newChars[newStrIndex++] = c; } else { newChars[newStrIndex++] = '%'; newChars[newStrIndex++] = '2'; newChars[newStrIndex++] = '0'; } } return new String(newChars); } public static void main(String[] args) { String input = "Hello Google"; String output = replaceSpace(input); System.out.println(output); // 输出: Hello%20Google } } ``` ### 使用 C++ 替换空格 在 C++ 中,可以通过遍历字符串并逐字符处理来实现空格替换。以下是一个示例,将空格替换为 `%20`: ```cpp #include <iostream> #include <string> using namespace std; string replaceSpace(string input) { int spaceCount = 0; for (char c : input) { if (c == ' ') { spaceCount++; } } int newLength = input.length() + 2 * spaceCount; string result(newLength, ' '); int index = 0; for (char c : input) { if (c != ' ') { result[index++] = c; } else { result[index++] = '%'; result[index++] = '2'; result[index++] = '0'; } } return result; } int main() { string input = "Hello Google"; string output = replaceSpace(input); cout << output << endl; // 输出: Hello%20Google return 0; } ``` ### 删除空格 如果目标是删除空格而不是替换,可以使用 `replaceAll()` 方法直接删除所有空格: ```java String input = "Hello Google"; String output = input.replaceAll(" ", ""); System.out.println(output); // 输出: HelloGoogle ``` 如果需要手动实现删除空格的功能,可以通过遍历字符数组并过滤空格: ```java public class RemoveSpaces { public static String removeSpaces(String input) { char[] originChars = input.toCharArray(); char[] newChars = new char[originChars.length]; int newStrIndex = 0; for (char c : originChars) { if (c != ' ') { newChars[newStrIndex++] = c; } } return new String(newChars, 0, newStrIndex); } public static void main(String[] args) { String input = "Hello Google"; String output = removeSpaces(input); System.out.println(output); // 输出: HelloGoogle } } ``` ### 总结 无论使用哪种语言,替换或删除空格的核心思想是遍历字符串中的每个字符,并根据需求进行处理。Java 提供了便捷的内置方法,而 C++ 则需要手动实现。对于更复杂的场景,例如替换为多个字符,手动实现可以提供更高的灵活性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值