空格替换

请编写一个方法,将字符串中的空格全部替换为“%20”。假定该字符串有足够的空间存放新增的字符,并且知道字符串的真实长度(小于等于1000),同时保证字符串由大小写的英文字母组成。
给定一个string iniString 为原始的串,以及串的长度 int len, 返回替换后的string。
测试样例:
“Mr John Smith”,13
返回:”Mr%20John%20Smith”
”Hello World”,12
返回:”Hello%20%20World”

class Replacement {
public:
    string replaceSpace(string iniString, int length) {

       string s1;
               if(length <= 0)
        {
            return s1;
        }

        s1 = iniString;
        string s2 = " ";
        int len =s2.length(); 
        int off = 0;
        off = s1.find(s2, 0);
        while (off != string:: npos)
        {
            s1.replace(off, len ,"%20");
            off = s1.find(s2, off + 1);
        }

        return s1;

    }
};

class Replacement {
public:
    string replaceSpace(string iniString, int length) {
     while (iniString.find(" ") != -1){
        iniString = iniString.replace(iniString.find(' '), 1, "%20");
    }
     return iniString;
    }
};

题目描述
请你实现一个简单的字符串替换函数。原串中需要替换的占位符为”%s”,请按照参数列表的顺序一一替换占位符。若参数列表的字符数大于占位符个数。则将剩下的参数字符添加到字符串的结尾。

给定一个字符串A,同时给定它的长度n及参数字符数组arg,请返回替换后的字符串。保证参数个数大于等于占位符个数。保证原串由大小写英文字母组成,同时长度小于等于500。

测试样例:
“A%sC%sE”,7,[‘B’,’D’,’F’]
返回:”ABCDEF”

class StringFormat {
public:
    string formatString(string A, int n, vector<char> arg, int m) {
        // write code here
        string res;
        if(A.size() != n || arg.size()!= m)
            return res;
        int i = 0;

        /*while(A.find("%s") != -1)
        {
             char a[2] = {arg[i++],0};
            string b = a;
          A.replace(A.find("%s"), 2, b);
        }*/
        while(A.find("%s") != -1)
        {
          A.replace(A.find("%s"), 2, 1,arg[i++]);
        }
        while(i<m)
        {
            A += arg[i++];
        }
        return A;
    }
};
在编中,字符串中的空格替换或删除是常见的需求,尤其是在处理输入数据或格式化输出时。以下是几种实方式,涵盖 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++ 则需要手动实。对于更复杂的场景,例如替换为多个字符,手动实可以提供更高的灵活性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值