// 文字列の後方WhiteSpaceを削除する。 // MFCの CString::TrimRight()みないな.. #include <iostream> #include <string> using namespace std; // ret 0 : normal int trim_right( string &str ) { string::size_type pos = str.find_last_not_of( " \t" ); if ( pos == str.npos ) { // not found return 1; } if ( pos == str.size() - 1 ) { // hit , but end. return 1; } str.erase( pos + 1 ); return 0; } int main() { string szStr = "hogehogeahihaslajfd \t "; cout << ":" << szStr << ":" << endl; trim_right( szStr ); cout << "string is :" << szStr << ":" << endl; return 0; } /* 指定文字群以外の文字が後方から検索して( find_last_not_of() )、 最初に現れる位置を知る。 それ以降を削除( erase() )すれば、trim_rightのできあがり。 */ // end of file