• <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日 ... 不鳥他們!!! 我要用自己開發的分布式文件系統、分布式調度系統、分布式檢索系統, 做自己的搜索引擎!!!大魚有大志!!! ---楊書童

            【轉】STL_string的字符串替換函數

            標準C++中的string中的函數不多,沒有CString的功能強大,但是如果想在Unicode編碼下使用多字節,就不能使用CString,于是自己寫了一個類似于CString的Replace函數。
            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;
            }

            上述方法在執行replace( "h h h h h h h h h h h h h h h h h h h ", " ", " " )時出現問題。
            下面再列出一種方法:
            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;
            }

            補充,經過測試,上面方法再執行,replace( “暴”, "\\","==" )時,依然會遇到問題。
            在日文系統上,因為“暴”占兩個字節,而"\\"只占一個字節,但與“暴”的低位字節ASCII碼相同。
            而string的Find函數,是按照字節比較的,所以,將這個字節替換了,導致文本替換出現問題。
            于是考慮到不應該按字節比較,應該按字符比較,測試發現,CString的替換函數沒有問題,于是考慮按照CString的方法重新寫一個replace函數。
            代碼如下:
            因為CString在_MBCS和_UNICODE下是變寬的,而我寫的replace函數,只針對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;

                // 先列出判斷替換字符是否存在的方法, 但在此函數中不使用這段代碼。
            /*
                // 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函數,在"MBSTRING.H"頭文件中聲明。

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

            麻豆久久久9性大片| 久久久婷婷五月亚洲97号色| 国产毛片久久久久久国产毛片 | 久久w5ww成w人免费| 四虎国产永久免费久久| 日本欧美国产精品第一页久久| 亚洲欧洲久久av| 国产亚洲美女精品久久久久狼| 久久久久久久久久久免费精品| 亚洲AV日韩精品久久久久久久| 国产综合免费精品久久久| 亚洲AV无码久久精品蜜桃| 国产农村妇女毛片精品久久| 久久亚洲国产成人精品性色 | 狠狠人妻久久久久久综合蜜桃| 国内精品伊人久久久久妇| 亚洲综合久久综合激情久久| 嫩草伊人久久精品少妇AV| 亚洲国产成人精品久久久国产成人一区二区三区综| 久久久久久午夜精品| 国产成人久久精品二区三区| 精品久久久久中文字幕日本| 亚洲?V乱码久久精品蜜桃| 99久久精品这里只有精品| 久久99久久99精品免视看动漫| 久久午夜无码鲁丝片秋霞 | 99久久国产亚洲高清观看2024| 国产毛片欧美毛片久久久| 久久久久国产成人精品亚洲午夜| 99久久精品影院老鸭窝| 国产午夜福利精品久久2021| 国内精品伊人久久久久777| 久久久久国产精品麻豆AR影院 | 青青青青久久精品国产h久久精品五福影院1421 | 77777亚洲午夜久久多喷| 亚洲综合熟女久久久30p| 欧美成人免费观看久久| 7777久久久国产精品消防器材| 久久国产免费直播| 性欧美大战久久久久久久久| 午夜久久久久久禁播电影|