C++中如何去掉std::string对象的首尾空格
?
string::find_first_not_of
string::find_last_not_of
這兩個可以去除首尾的空格
?
?
?/***********begin test file***********/
#include <iostream>
#include <string>
int main()
{
?std::string str1 = "???hello world!?? ";
?std::string trimstring = " ";
?std::cout << "str = \"" << str1 << "\"" << std::endl;
?std::cout << "str.find_first_of(' ')???? : " << str1.find_first_of(trimstring)???? << std::endl;
?std::cout << "str.find_first_not_of(' ') : " << str1.find_first_not_of(trimstring) << std::endl;
?std::cout << "str.find_last_of(' ')???? : " << str1.find_last_of(trimstring)????? << std::endl;
?std::cout << "str.find_last_not_of(' ')? : " << str1.find_last_not_of(trimstring)? << std::endl;
?str1.erase(str1.find_last_not_of(trimstring)+1);
?std::cout << "after right trim : \"" << str1 << "\"" << std::endl;
?str1.erase(0,str1.find_first_not_of(trimstring));
?std::cout << "after left trim? : \"" << str1 << "\"" << std::endl;
?return 0;
}
/***********end test file***********/
轉載于:https://blog.51cto.com/wellwy/582628
總結
以上是生活随笔為你收集整理的C++中如何去掉std::string对象的首尾空格的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android版同步工具豌豆荚实测 电脑
- 下一篇: 网闸中的核心技术