【工具类】C++花式字符串操作

  • 分割字符串
void Separate(const wstring& src, const wstring& strSet, vector& container)
{
    wstring::size_type pos = 0;
    pos = src.find_first_not_of(strSet, pos);

    while(pos != wstring::npos)
    {
        string::size_type next = src.find_first_of(strSet, pos);
        wstring token = src.substr(pos, next-pos);
        container.push_back(token);
        pos = next;

        if(pos != wstring::npos)
        {
            ++pos;
        }

        if(pos >= src.size()) 
        {
            pos = wstring::npos;
        }
    }
}
  • 字符串替换
wstring Replace(const wstring& src, const wstring& beforeList, wchar_t after)
{
    if(beforeList.length() == 0) return src;

    wstring srcTemp = src;

    wstring::size_type pos = 0;
    while((pos = srcTemp.find_first_of(beforeList, pos)) != wstring::npos)
    {
        srcTemp.replace(pos, 1, 1, after);
        pos += 1;
    }

    return srcTemp;
}
  • 全角转半角
wstring ConvertSbcToDbc(const wstring& src)
{
    wstring strTemp = src;

    static const wchar_t SBC_START = 65281;
    static const wchar_t SBC_END = 65374;
    static const wchar_t SBC_BASE = 65248;
    static const wchar_t SBC_SPACE = 12288;
    static const wchar_t DBC_SPACE = ' ';

    wstring::size_type len =  strTemp.size();
    for(wstring::size_type i = 0; i < len; ++i)
    {
        if(SBC_START <= strTemp[i] && strTemp[i] <= SBC_END)
        {
            strTemp[i] -= SBC_BASE;
        }
        else if(strTemp[i] == SBC_SPACE)
        {
            strTemp[i] = DBC_SPACE;
        }
    }

    return strTemp;
}
  • 判断是否为中文字符
bool IsChChar(wchar_t inCh)
{
    unsigned short dBegin =  0x4e00;
    unsigned short dEnd = 0x9fff;


    unsigned short dw = (unsigned short)inCh;

    if(dw >= dBegin && dw <= dEnd)
    {
        return true;
    }

    return false;

}
  • Returns 字符串 \a src 中从 \a begin 开始到 \a end 之间的字符,不包括开始和结束字符.
wstring Slice(const wstring& src, const wstring& begin, const wstring& end)
{
    wstring strResult;

    size_t posStart = src.find(begin);
    if(posStart == wstring::npos)
    {
        return L"";
    }

    size_t posEnd = src.find(end);
    if(posEnd == wstring::npos)
    {
        return L"";
    }

    if(posEnd <= posStart) 
    {
        return L"";
    }

    return src.substr(posStart + begin.length(), posEnd - posStart - begin.length());
}
  • 判断字符串是否以什么结尾
/*!
    Returns 字符串 \a src 是否以指定的串 \a dest 结束.
*/
bool EndWith(const wstring& src, const wstring& dest)
{
    if(src.size() < dest.size()) return false;

    size_t nSrcLen = src.length();
    size_t nDestLen = dest.length();
    for(size_t i = 0; i < nDestLen; ++i)
    {
        if(src[nSrcLen - nDestLen + i] == dest[i]) continue;

        return false;
    }

    return true;
}
  • 判断是否为纯数字
bool IsAllNumber(const wstring& src)
{
    unsigned short dBegin =  0x0030;
    unsigned short dEnd = 0x0039;

    if(src.size() < 1) return false;

    size_t nSrcLen = src.length();

    for(size_t i = 0; i < nSrcLen; ++i)
    {
        if(src[i] < dBegin || src[i] > dEnd) return false;
    }

    return true;
}
  • 判断是否为全英文
bool IsAllLetter(const wstring& src)
{
    unsigned short dCapitalBegin =  0x0041;//"A"
    unsigned short dCapitalEnd = 0x005a;

    unsigned short dMinBegin = 0x0061;
    unsigned short dMinEnd = 0x007a;

    if(src.size() < 1) return false;

    size_t nSrcLen = src.length();

    for(size_t i = 0; i < nSrcLen; ++i)
    {
        if(!((src[i] >= dMinBegin && src[i] <= dMinEnd) || (src[i] >= dCapitalBegin && src[i] <= dCapitalEnd))) return false;
    }

    return true;
}
  • 中文转成拼音
wstring ChineseCharToPhoneticize(wstring src)
{
    string strCode;
    StringUtility::W2AString(src, strCode);

    size_t destSize = strlen(strCode.c_str())+1;
    char* as_HzString = new char[destSize];
    strcpy_s(as_HzString, destSize, strCode.c_str());

    static int li_SecPosValue[]={1601,1637,1833,2078,2274,2302,2433,2594,2787,3106,3212,3472,3635,3722,3730,3858,4027,4086,4390,4558,4684,4925,5249}; 
    static char* lc_FirstLetter[] ={"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"}; 
    static char* ls_SecondSecTable = 
        "CJWGNSPGC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as_HzString); 

    for(unsigned i = 0; i < stringlen; i++)   
    { 
        H = (unsigned char)(as_HzString[i]); 
        L = (unsigned char)(as_HzString[i+1]); 
        if(H < 0xA1 || L < 0xA1)
        { 
            result += as_HzString[i]; 
            continue; 
        } 
        else
        {
            W = (H - 160) * 100 + L - 160;
        }

        if(W > 1600 && W < 5590)   
        { 
            for(int j = 22; j >= 0; j--) 
            {
                if(W >= li_SecPosValue[j])   
                { 
                    result += lc_FirstLetter[j]; 
                    i++; 
                    break; 
                } 
                continue; 
            }
        } 
        else 
        { 
            i++; 
            W = (H - 160 - 56)*94 + L - 161; 
            if(W >= 0 && W <= 3007) 
            {
                result += ls_SecondSecTable[W]; 
            }
            else   
            { 
                result += (char)H; 
                result += (char)L; 
            } 
        } 
    } 

    wstring strPY;
    StringUtility::A2Wstring(result, strPY);

    StringUtility::Replace<wstring>(strPY, L"*", L"");
    StringUtility::Replace<wstring>(strPY, L"(", L"");
    StringUtility::Replace<wstring>(strPY, L")", L"");
    StringUtility::Replace<wstring>(strPY, L"、", L"");

    return strPY; 
}
  • 十六进制字符串转长整型.
long HexWstringToLong(const wstring& strHex)
{   
    char * strTemp = WideStringToMBS(strHex.c_str());
    long longTemp = strtol(strTemp,NULL,16);    
    return longTemp;
}
  • 十六进制字符串颜色值转换成RGB
bool HexColorToRGB(const wstring& strHexColor, int &nR, int &nG, int &nB)
{
    wstring strHexClr = strHexColor;

    if(strHexClr.length() < 7 || strHexClr.length() > 8)
    {
        return false;
    }
    if(strHexClr.find(L"#") != wstring::npos)   
    {
        strHexClr = strHexClr.substr(1, strHexClr.length() - 1);        
        long longTemp = HexWstringToLong(strHexClr);   
        nR = (longTemp & 0xFF0000 )>>16;    
        nG = (longTemp & 0x00FF00 )>>8;
        nB =  longTemp & 0x0000FF;  

        return true;
    }

    if(strHexClr.find(L"0x") != wstring::npos)
    {
        strHexClr = strHexClr.substr(2, strHexClr.length() - 1);        
        long longTemp = HexWstringToLong(strHexClr);     
        nR = (longTemp & 0xFF0000 )>>16;    
        nG = (longTemp & 0x00FF00 )>>8;
        nB =  longTemp & 0x0000FF;  

        return true;
    }
    return false;
}
  • 将strSrc字符串中的nBegin到nEnd中的字符删掉(不包括两边字符)
wstring Truncate(wstring& strSrc, const int nBegin, const int nEnd)
{
    if (nBegin < 0 || nEnd < 0 || nBegin > nEnd)
    {
        return strSrc;
    }

    wstring strDec;
    for (wstring::size_type i = strSrc.size(); i > 0; i--)
    {
        if(i > nBegin && i < nEnd)
            continue;
        strDec = strSrc[i-1] + strDec;
    }
    return strDec;
}

你可能感兴趣的:(编程笔记)