わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

wstringの前後の空白をトリミングする

文字列の前後の空白をトリミングします。

wstringの前後の空白をトリミングする
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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"






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。