【问题标题】:Remove spaces before the first word in string in C++ [duplicate]在C ++中删除字符串中第一个单词之前的空格[重复]
【发布时间】:2017-04-13 22:05:45
【问题描述】:

我知道如何在 C 中删除字符串中第一个单词之前的一个或多个空格,但我不知道在 C++ 中(如果有示例函数)。

我的字符串是:“Hello”,我想得到“Hello”。我该怎么办?

【问题讨论】:

  • boost::trim_left 那是 c++ 怎么样

标签: c++ string


【解决方案1】:

使用std::string::find_first_not_of(' ') 获取第一个非空白字符的索引,然后从那里取出子字符串

例子:

std::string str = " Hello";
auto pos = str.find_first_not_of(' ');
auto Trimmed = str.substr(pos != std::string::npos ? pos : 0);

std::string TrimLeft(const std::string& str){
    auto pos = str.find_first_not_of(' ');
    return str.substr(pos != std::string::npos ? pos : 0);
}

【讨论】:

    猜你喜欢
    • 2016-04-26
    • 2021-06-30
    • 2015-06-08
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 2013-04-14
    • 2015-02-08
    • 2015-12-22
    相关资源
    最近更新 更多