VC中支持中文的字符串比较函数

VS2008开发环境,多字符集和UNICODE字符集都可用。

WCHAR * mbcsToUnicode(const char *zStr)  
{  
    int nByte;  
    WCHAR *zMbcsStr;  
    int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;  
    nByte = MultiByteToWideChar(codepage, 0, zStr, -1, NULL,0)*sizeof(WCHAR);  
    zMbcsStr = (WCHAR*)malloc( nByte*sizeof(zMbcsStr[0]) );  
    if( zMbcsStr==0 )  
        {  
        return 0;  
    }  
    nByte = MultiByteToWideChar(codepage, 0, zStr, -1, zMbcsStr, nByte);  
    if( nByte==0 )  
        {  
        free(zMbcsStr);  
        zMbcsStr = 0;  
    }  
    return zMbcsStr;  
}  
  
BOOL CompareStr(CStringA S1, CStringA S2)  
{  
    WCHAR* wa;  
    WCHAR* wb;  
    wa = mbcsToUnicode(S1.GetBuffer());  
    wb = mbcsToUnicode(S2.GetBuffer());  
    return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, (LPCWSTR)wa, wcslen(wa), (LPCWSTR)wb, wcslen(wb)) - CSTR_EQUAL == 1;  
}

参考:http://blog.csdn.net/dbyoung/article/details/7716086

你可能感兴趣的:(字符串)