将空白字符替换为 %20 的字符串,忽略所有尾随空格
String Replacement of Blank character with %20 neglecting all trailing blank spaces
我想用%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++中的所有样板(append
,at
整数循环(。
你试过吗:
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()
来构建我们的字符串。
相关文章:
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 移除前面有空格的前2个字符串
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 在字符串中插入空格
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 使用另一个字符串从字符串中删除空格
- 我还可以添加什么来按空格或空格正确拆分C++字符串?
- C++ "noskipws"没有按预期工作,如何正确允许字符串中的空格?
- 在 c++ 中逐行阅读字符串,不要以空格停止
- std::regex:匹配由数字和空格组成的字符串,并提取数字.如何?
- 如何存储包含多个空格的字符串 c++
- 在特定位置为字符串添加空格
- 有没有办法在 c++ 中获取带有空格的完整字符串?
- C++完全复制要字符串的字符列表,而不会忽略多个空格
- Winsock2:当我尝试发送带有空格的字符串时,该函数在遇到空格时似乎停止发送
- 从没有空格的字符串中提取整数
- 用于拆分空格字符串的程序不起作用
- 解析C++中的完整(从头到尾)字符串,包括所有空格
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- 如何获取我的区域设置的空格字符串?