【发布时间】:2017-04-13 22:05:45
【问题描述】:
我知道如何在 C 中删除字符串中第一个单词之前的一个或多个空格,但我不知道在 C++ 中(如果有示例函数)。
我的字符串是:“Hello”,我想得到“Hello”。我该怎么办?
【问题讨论】:
-
boost::trim_left那是 c++ 怎么样
我知道如何在 C 中删除字符串中第一个单词之前的一个或多个空格,但我不知道在 C++ 中(如果有示例函数)。
我的字符串是:“Hello”,我想得到“Hello”。我该怎么办?
【问题讨论】:
boost::trim_left 那是 c++ 怎么样
使用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);
}
【讨论】: