首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从字符串中移除前导和尾随空格

从字符串中移除前导和尾随空格
EN

Stack Overflow用户
提问于 2009-11-25 16:22:37
回答 26查看 245.3K关注 0票数 116

如何从C++中的字符串对象中删除空格。

例如,如何从下面的string对象中移除前导和尾随空格。

代码语言:javascript
复制
//Original string: "         This is a sample string                    "
//Desired string: "This is a sample string"

据我所知,string类没有提供任何方法来删除前导和尾随空格。

要增加此问题,如何扩展此格式以处理字符串的单词之间的额外空格。例如,

代码语言:javascript
复制
// Original string: "          This       is         a sample   string    " 
// Desired string:  "This is a sample string"  

使用解决方案中提到的字符串方法,我可以考虑分两个步骤执行这些操作。

  1. 拆卸前导和尾随空格。
  2. 使用find_first_of、find_last_of、find_first_not_of、find_last_not_of和substr,在word边界上反复使用以获得所需的格式。
EN

回答 26

Stack Overflow用户

回答已采纳

发布于 2009-11-25 16:29:10

这叫修剪。如果您可以使用助推,我会推荐它。

否则,使用find_first_not_of获取第一个非空白字符的索引,然后使用find_last_not_of从末尾获得不是空格的索引。使用这些,使用substr获得没有周围空格的子字符串。

作为对您编辑的回应,我不知道这个词,但我会猜到一些类似于“减少”的东西,所以我就是这样称呼它的。:) (注意,为了灵活起见,我已将空白更改为参数)

代码语言:javascript
复制
#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;
}

结果:

代码语言:javascript
复制
[too much               space]  
[too much space]  
[too-much-space]  
[one  
two]  
票数 142
EN

Stack Overflow用户

发布于 2014-02-16 18:23:55

在一行中很容易地从std::string中移除前导、尾随和额外空格

代码语言:javascript
复制
value = std::regex_replace(value, std::regex("^ +| +$|( ) +"), "$1");

只移除前导空格

代码语言:javascript
复制
value.erase(value.begin(), std::find_if(value.begin(), value.end(), std::bind1st(std::not_equal_to<char>(), ' ')));

代码语言:javascript
复制
value = std::regex_replace(value, std::regex("^ +"), "");

只移除尾随空间

代码语言:javascript
复制
value.erase(std::find_if(value.rbegin(), value.rend(), std::bind1st(std::not_equal_to<char>(), ' ')).base(), value.end());

代码语言:javascript
复制
value = std::regex_replace(value, std::regex(" +$"), "");

只删除额外的空间

代码语言:javascript
复制
value = regex_replace(value, std::regex(" +"), " ");
票数 58
EN

Stack Overflow用户

发布于 2014-09-14 01:14:59

我目前正在使用这些功能:

代码语言:javascript
复制
// 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);
}
票数 52
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1798112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档