2 c++
我编写了以下 C++ 代码,它从字符串的开头和结尾删除空格,但问题是它没有删除制表符,我该如何解决?
另外,有没有类似于制表符和空格的东西?(我正在从文件中读取行)
string trim_edges(string command) {
const auto command_begin = command.find_first_not_of(' ');
if (command_begin == std::string::npos)
return "";
const auto strEnd = command.find_last_not_of(' ');
const auto strRange = strEnd - command_begin + 1;
return command.substr(command_begin, strRange);
}
Run Code Online (Sandbox Code Playgroud)
find_first_not_of并且find_last_not_of还可以将字符串作为一组字符来跳过:
const auto command_begin = command.find_first_not_of(" \t");
const auto strEnd = command.find_last_not_of(" \t");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
256 次 |
| 最近记录: |