C++字符分割

AfxExtractSubString

表頭: <afxwin.h>html

BOOL AFXAPI AfxExtractSubString ( CString& rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep = '\n');app

參數

rString 對CString將獲得一個單獨的子字符串的對象。函數

lpszFullString 字符串包含字符串的全文提取自。.net

iSubString 提取的子字符串的從零開始的索引從lpszFullString。code

chSep 使用的分隔符分隔子字符串,默認的是'\n'。htm

返回值

TRUE ,若是函數成功提取了該子字符串中提供的索引;不然, FALSE。對象

例:

// 使用AfxExtractSubString分割字符串
void CSplitString::SplitString1()
{
    std::vector<long> arrPic;

    CString strContent = _T("1,2,3,4,5");
    CString strTemp;
    int iPos = 0;

    while (AfxExtractSubString(strTemp, strContent, iPos, ','))
    {
        iPos++;
        arrPic.push_back(_wtol(strTemp));
    }
}

STL find_first_of

// 利用STL本身實現字符串分割
void CSplitString::SplitString2()
{
    const std::string s("1,2,3,4,5;6;7;8;9");
    std::vector<std::string> v;
    const std::string c(",;");//多個分隔符

    std::string::size_type pos1, pos2;
    pos2 = s.find_first_of(c);
    pos1 = 0;
    while (std::string::npos != pos2)
    {
        v.push_back(s.substr(pos1, pos2 - pos1));

        pos1 = pos2 + 1;
        pos2 = s.find_first_of(c, pos1);
    }
    if (pos1 != s.length())
        v.push_back(s.substr(pos1));
}

_tcstok_s

// 使用C的_tcstok分割字符串
void CSplitString::SplitString3()
{
    CString str = _T("a,b*c,d");
    TCHAR seps[] = _T(",*");//可按多個字符來分割
    TCHAR *next_token1 = NULL;
    TCHAR* token = _tcstok_s((LPTSTR)(LPCTSTR)str, seps,&next_token1);
    while (token != NULL)
    {
        TRACE("\r\nstr=%s  token=%s\r\n", str, token);
        token = _tcstok_s(NULL, seps, &next_token1);
    }
}

http://www.qiezichaodan.com/mfc_cstring_split/blog

http://blog.csdn.net/xjw532881071/article/details/49154911索引

http://www.cnblogs.com/happykoukou/p/5427268.htmltoken

參考:

相關文章
相關標籤/搜索