• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            不會飛的鳥

            2010年12月10日 ... 不鳥他們!!! 我要用自己開發(fā)的分布式文件系統(tǒng)、分布式調度系統(tǒng)、分布式檢索系統(tǒng), 做自己的搜索引擎!!!大魚有大志!!! ---楊書童

            【轉】STL_string的字符串替換函數(shù)

            標準C++中的string中的函數(shù)不多,沒有CString的功能強大,但是如果想在Unicode編碼下使用多字節(jié),就不能使用CString,于是自己寫了一個類似于CString的Replace函數(shù)。
            string replace( const string& inStr, const char* pSrc, const char* pReplace )
            {
                 string str = inStr;
                string::size_type stStart = 0;
                string::iterator iter = str.begin();
                while( iter != str.end() )
                {
                    // 從指定位置 查找下一個要替換的字符串的起始位置。
                    string::size_type st = str.find( pSrc, stStart );
                    if ( st == str.npos )
                    {
                        break;
                    }
                    iter = iter + st - stStart;
                    // 將目標字符串全部替換。
                    str.replace( iter, iter + strlen( pSrc ), pReplace );
                    iter = iter + strlen( pReplace );
                    // 替換的字符串下一個字符的位置
                    stStart = st + strlen( pReplace );
                }
                return str;
            }

            上述方法在執(zhí)行replace( "h h h h h h h h h h h h h h h h h h h ", " ", " " )時出現(xiàn)問題。
            下面再列出一種方法:
            string CComFunc::replace( const string& inStr, const char* pSrc, const char* pReplace )
            {
                string strSrc = inStr;
                string::size_type pos=0;      
                string::size_type srclen = strlen( pSrc );       
                string::size_type dstlen = strlen( pReplace );       
                while( (pos=strSrc.find(pSrc, pos)) != string::npos)
                {               
                    strSrc.replace(pos, srclen, pReplace);               
                    pos += dstlen;       
                }
                return strSrc;
            }

            補充,經(jīng)過測試,上面方法再執(zhí)行,replace( “暴”, "\\","==" )時,依然會遇到問題。
            在日文系統(tǒng)上,因為“暴”占兩個字節(jié),而"\\"只占一個字節(jié),但與“暴”的低位字節(jié)ASCII碼相同。
            而string的Find函數(shù),是按照字節(jié)比較的,所以,將這個字節(jié)替換了,導致文本替換出現(xiàn)問題。
            于是考慮到不應該按字節(jié)比較,應該按字符比較,測試發(fā)現(xiàn),CString的替換函數(shù)沒有問題,于是考慮按照CString的方法重新寫一個replace函數(shù)。
            代碼如下:
            因為CString在_MBCS和_UNICODE下是變寬的,而我寫的replace函數(shù),只針對string。
            string CComFunc::replace( const string& inStr, const char* pSrc, const char* pReplace )
            {
                string strSrc = inStr;
                LPSTR lpch = ( CHAR* )strSrc.c_str();
                int   nOldLength = strlen( lpch );
                int    nSourceLen = strlen(pSrc);
                if (nSourceLen == 0)
                {
                    return lpch;
                }
                int   nReplacementLen = strlen(pReplace);
                LPSTR lpszStart = lpch;
                LPSTR lpszEnd = lpszStart + nOldLength;
                LPSTR lpszTarget;

                // 先列出判斷替換字符是否存在的方法, 但在此函數(shù)中不使用這段代碼。
            /*
                // judge whether exist
                while (lpszStart < lpszEnd)
                {
                    while ((lpszTarget = (CHAR*)_mbsstr(( const unsigned char * )lpszStart, ( const unsigned char * )pSrc)) != NULL)
                    {
                        nCount++;
                        lpszStart = lpszTarget + nSourceLen;
                    }
                    lpszStart += strStart.length() + 1;
                }
                *//
               
                // 下面是替換的代碼。
                while (lpszStart < lpszEnd)
                {
                    while ((lpszTarget = (CHAR*)_mbsstr(( const unsigned char * )lpszStart, ( const unsigned char * )pSrc)) != NULL)
                    {
                        int nBalance = nOldLength - (lpszTarget - lpch + nSourceLen);
                        memmove(lpszTarget + nReplacementLen, lpszTarget + nSourceLen,
                            nBalance * sizeof(CHAR));
                        memcpy(lpszTarget, pReplace, nReplacementLen*sizeof(CHAR));
                        lpszStart = lpszTarget + nReplacementLen;
                        lpszStart[nBalance] = '\0';
                        nOldLength += (nReplacementLen - nSourceLen);
                    }
                    lpszStart += strlen(lpszStart) + 1;
                }
                return lpch;
            }

            此方法最關鍵的是_mbsstr函數(shù),在"MBSTRING.H"頭文件中聲明。

            posted on 2012-10-26 16:20 不會飛的鳥 閱讀(1460) 評論(0)  編輯 收藏 引用

            久久亚洲国产欧洲精品一| 久久99久国产麻精品66| 久久水蜜桃亚洲av无码精品麻豆| 精品多毛少妇人妻AV免费久久 | 99久久亚洲综合精品成人| 久久国产精品77777| 99久久亚洲综合精品网站| 国产A级毛片久久久精品毛片| 久久99精品久久只有精品| 久久成人永久免费播放| 精品综合久久久久久98| 丁香五月综合久久激情| 亚洲精品无码久久久久| 久久综合九色综合久99 | 久久电影网一区| 99久久国产亚洲综合精品| 97久久精品人人澡人人爽| 无遮挡粉嫩小泬久久久久久久| 国产精品欧美亚洲韩国日本久久| 亚洲国产精品无码久久98| 久久精品无码免费不卡| 久久国产精品-久久精品| 亚洲精品乱码久久久久久自慰| 久久久久99精品成人片牛牛影视| 成人国内精品久久久久一区| 久久久亚洲裙底偷窥综合| 久久久国产精华液| 久久精品成人欧美大片| 伊人久久综在合线亚洲2019 | 国产精品成人99久久久久| 东京热TOKYO综合久久精品| 亚洲精品白浆高清久久久久久| 久久久这里有精品| 色99久久久久高潮综合影院| 久久AⅤ人妻少妇嫩草影院| 88久久精品无码一区二区毛片| 99精品久久精品| 日本精品久久久久中文字幕8| 9久久9久久精品| 国产高潮国产高潮久久久91| 国产99久久久国产精品~~牛|