将空白字符替换为 %20 的字符串,忽略所有尾随空格

String Replacement of Blank character with %20 neglecting all trailing blank spaces

本文关键字:空格 字符串 字符 空白 替换      更新时间:2023-10-16

我想用%20填充空格。下面的代码无法编译并在行中给出错误

new_String.append(url.at(j));`

法典:

void main()
{
    string url = "abcde";
    string new_String;
    for (int j = 0; j < url.length(); ++j)
    {
        if (url.at(j) == ' ')
        {
            new_String.append("%20");
        }
        else
            new_String.append(url.at(j));
    }

根据Stackoverflow用户给出的建议,我能够执行我的程序。下面的代码工作得很好。但是代码非常复杂(尤其是尾随空格的计算(。谁能给我一些建议,使代码更可行。程序接受输入字符串并用%20替换空白字符,但end中的空白字符应忽略,不应替换为%20

#include"iostream"
using namespace std;
#include"string"
void main()
{
    string url = "ta  nuj  ";
    int count = 1; // it holds the blank space present in the end
    for (int i = 0; i < url.length(); i++) // this is written to caculate the blank space that //are at the end
    {
        if (url.at(i) == ' ')
        {
            for (int k = i + 1; k < url.length(); k++)
            {
                if ((int)url.at(k) != 32)
                {
                    count = 1;
                    break;
                }
                else
                {
                    count++;
                    i++;
                }
            }
        }
    }
        string newUrl;
        for (int j = 0; j < url.length()-count; j++)
        {
            if (url.at(j) == ' ')
            {
                newUrl.append("%20");
            }
            else
                newUrl += (url[j]);
        }
        cout << "nThe replaced url is:" << newUrl;
}

打开手册会告诉你string::append()只接受一个字符串作为输入,所以你可能想要:

new_String += string(url[j]);

作为旁注,你不需要现代C++中的所有样板(appendat整数循环(。

你试过吗:

newUrl.append(1, url.at(j));

谁能给我一些建议,使代码更可行?

如果这是问题,答案是使用算法函数轻松做到这一点。

首先,您应该修剪尾随空格的字符串。 其次,通过遍历原始字符串来构建新字符串,并在循环时测试空格字符。

下面说明了这两个概念:

#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>
#include <functional>
using namespace std;
// A function object that will take a character, and append either "%20" or the 
// original character to our new string    
struct Converter
{
    std::string* pS;  // this points to our string we will build
    Converter(std::string* s) : pS(s) {}
    void operator()(char ch) 
    {
        if ( isspace(ch) )
            (*pS) += "%20";   // it's a space, so replace
        else
            *pS += ch;    // it is not a space, so use the same character
    }
};
// trim spaces from end of string
std::string &rtrim(std::string &s) 
{
    s.erase(std::find_if(s.rbegin(), s.rend(), 
            std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
    return s;
}
// function to return the new string by looping over the original string 
string getNewString(const std::string& s)
{
    std::string outStr;  // our new string
    // loop over each character, calling our Coverter::operator()(char) function
    for_each(s.begin(), s.end(), Converter(&outStr));
    return outStr;
}
int main()
{
    string instring = "This string has spaces ";
    cout << getNewString(rtrim(instring));
}

输出:

This%20string%20has%20spaces

现场示例:http://ideone.com/kXPKgU

请注意,SO上的此链接提供了修剪std::string的最佳方法是什么?的答案。

for_each是一个算法函数,基本上是一个循环。 循环的每次迭代都会调用函数、函数对象或 lambda(如果使用 C++11(。 由于在这种情况下我们使用的是一个名为 Converter 的函数对象,我们重载了Converter operator()来构建我们的字符串。