文字列の前後の空白をトリミングします。
#include <tchar.h>
#include <iostream>
#include <string>
#include <windows.h>
/*
wstringのトリミング
*/
std::wstring TrimString
(
std::wstring strString // トリミングしたい文字列
, const WCHAR* pDelim = L" ¥t¥r¥n" // デリミタ
)
{
// 先頭からデリミタ以外を探す
int nFirst = strString.find_first_not_of( pDelim );
if ( std::wstring::npos == nFirst ) {
// デリミタしかなかった
return( L"" );
}
// 後方からデリミタ以外を探す
int nLast = strString.find_last_not_of( pDelim );
// 中間を返す
return( strString.substr( nFirst, nLast - nFirst + 1 ) );
}
/*
ブラウザコントロールの生成
*/
int _tmain
(
int argc
, _TCHAR* argv[]
)
{
// ロケール変更(wcoutでユニコードを出力する為)
std::wcout.imbue(std::locale("", std::locale::ctype));
// トリミング
std::wstring strRet = TrimString( L" abc def " );
// 結果の出力
std::wcout << L"¥"" << strRet.c_str() << L"¥"" << std::endl;
// 正常終了
return( 0 );
}
"abc def"