如何从C++中的字符串对象中删除空格。
例如,如何从下面的string对象中移除前导和尾随空格。
//Original string: " This is a sample string "
//Desired string: "This is a sample string"据我所知,string类没有提供任何方法来删除前导和尾随空格。
要增加此问题,如何扩展此格式以处理字符串的单词之间的额外空格。例如,
// Original string: " This is a sample string "
// Desired string: "This is a sample string" 使用解决方案中提到的字符串方法,我可以考虑分两个步骤执行这些操作。
发布于 2009-11-25 16:29:10
这叫修剪。如果您可以使用助推,我会推荐它。
否则,使用find_first_not_of获取第一个非空白字符的索引,然后使用find_last_not_of从末尾获得不是空格的索引。使用这些,使用substr获得没有周围空格的子字符串。
作为对您编辑的回应,我不知道这个词,但我会猜到一些类似于“减少”的东西,所以我就是这样称呼它的。:) (注意,为了灵活起见,我已将空白更改为参数)
#include <iostream>
#include <string>
std::string trim(const std::string& str,
const std::string& whitespace = " \t")
{
const auto strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content
const auto strEnd = str.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
std::string reduce(const std::string& str,
const std::string& fill = " ",
const std::string& whitespace = " \t")
{
// trim first
auto result = trim(str, whitespace);
// replace sub ranges
auto beginSpace = result.find_first_of(whitespace);
while (beginSpace != std::string::npos)
{
const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
const auto range = endSpace - beginSpace;
result.replace(beginSpace, range, fill);
const auto newStart = beginSpace + fill.length();
beginSpace = result.find_first_of(whitespace, newStart);
}
return result;
}
int main(void)
{
const std::string foo = " too much\t \tspace\t\t\t ";
const std::string bar = "one\ntwo";
std::cout << "[" << trim(foo) << "]" << std::endl;
std::cout << "[" << reduce(foo) << "]" << std::endl;
std::cout << "[" << reduce(foo, "-") << "]" << std::endl;
std::cout << "[" << trim(bar) << "]" << std::endl;
}结果:
[too much space]
[too much space]
[too-much-space]
[one
two] 发布于 2014-02-16 18:23:55
在一行中很容易地从std::string中移除前导、尾随和额外空格
value = std::regex_replace(value, std::regex("^ +| +$|( ) +"), "$1");只移除前导空格
value.erase(value.begin(), std::find_if(value.begin(), value.end(), std::bind1st(std::not_equal_to<char>(), ' ')));或
value = std::regex_replace(value, std::regex("^ +"), "");只移除尾随空间
value.erase(std::find_if(value.rbegin(), value.rend(), std::bind1st(std::not_equal_to<char>(), ' ')).base(), value.end());或
value = std::regex_replace(value, std::regex(" +$"), "");只删除额外的空间
value = regex_replace(value, std::regex(" +"), " ");发布于 2014-09-14 01:14:59
我目前正在使用这些功能:
// trim from left
inline std::string& ltrim(std::string& s, const char* t = " \t\n\r\f\v")
{
s.erase(0, s.find_first_not_of(t));
return s;
}
// trim from right
inline std::string& rtrim(std::string& s, const char* t = " \t\n\r\f\v")
{
s.erase(s.find_last_not_of(t) + 1);
return s;
}
// trim from left & right
inline std::string& trim(std::string& s, const char* t = " \t\n\r\f\v")
{
return ltrim(rtrim(s, t), t);
}
// copying versions
inline std::string ltrim_copy(std::string s, const char* t = " \t\n\r\f\v")
{
return ltrim(s, t);
}
inline std::string rtrim_copy(std::string s, const char* t = " \t\n\r\f\v")
{
return rtrim(s, t);
}
inline std::string trim_copy(std::string s, const char* t = " \t\n\r\f\v")
{
return trim(s, t);
}https://stackoverflow.com/questions/1798112
复制相似问题