删除 C++ 中字符串中第一个单词之前的空格
Remove spaces before the first word in string in C++
我知道如何在 C 中删除字符串中第一个单词之前的一个或多个空格,但我不知道C++(如果有示例函数(。
我的字符串是:"Hello",我想得到"Hello"。我该怎么办?
使用 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);
}
相关文章:
- C++为一串单词添加空格
- 在单词之间添加空格
- 反转字符串中单词的位置,而不更改 O(1) 空格限制中特殊字符的顺序
- 单词exp和带空格的字符串
- 单词之间的莫尔斯电码空格问题
- 当字符串中只有一个"GOOD"字符串可用时,从没有空格的字符串中提取第一个单词的最佳和有效方法?
- 如何用C 中的文件中忽略空格读单词
- C++ 在空格不规则的字符串中迭代单词
- 如何在两个单词之间添加空格
- 在字符串中找到单词出现数量的最佳方法(C ,字符串中没有空格)
- 删除 C++ 中字符串中第一个单词之前的空格
- 匹配由符号分隔的重复单词,忽略空格
- 如何使用C 中的Regex在给定句子中的多个空格隔开的两个单词匹配
- C++均匀地分配单词之间的空格
- 增加单词之间的空格,按列排列单词
- 从字符串中读取单词C++,同时忽略空格、数字和符号
- 如何按空格循环字符串?我怎么知道字符串中我当前所在的单词的索引号
- 增强字符串拆分以消除单词中的空格
- 确定单词后跟的是空格还是换行符
- c ++ 如何提取单词之间的空格(如果有)