正文

C++中如何去掉std::string对象的首尾空格2008-07-01 21:13:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jixian/36400.html

分享到:

转自http://www.cnblogs.com/chenrj/archive/2006/11/14/560455.html

 

/***********write by myself***********/
/***********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***********/

阅读(4654) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册