CompareString
The CompareString function compares two character strings, using the specified locale.
- int CompareString(
- LCID Locale, // locale identifier
- DWORD dwCmpFlags, // comparison-style options
- LPCTSTR lpString1, // first string
- int cchCount1, // size of first string
- LPCTSTR lpString2, // second string
- int cchCount2 // size of second string
- );
参数:
此函数用来比较两字符串。第一个参数指定一个本地ID(LCID),它是32位的,确定一种特定语言。CompareString检查LCID所应用的特定语言的字符含义,来进行字符串比较。语言修正比较对最终用户产生更多的含义。但是这种比较方式比顺序比较慢。通过Windows的GetThreadLocale函数可以得到调用线程的本地ID:
- LCID GetThreadLocale();
CompareString的第二个参数标记出函数比较两字符串所使用的方法。图2-4列出可能的标记:
Flag | Meaning |
NORM_IGNORECASE LINGUISTIC_IGNORECASE | 忽略大小写。 |
NORM_IGNOREKANATYPE | 不区分平假名和片假名。 |
NORM_IGNORENONSPACE LINGUISTIC_IGNOREDIACRITIC | 忽略 nonspacing字符. |
NORM_IGNORESYMBOLS | 忽略符号。 |
NORM_IGNOREWIDTH | 不区分相同字符的单字节和双字节字符。 |
SORT_STRINGSORT | 将标点按符号处理。 |
返回值:
DEMO:
- #include <Windows.h>
- #include <tchar.h>
- #include <iostream>
- using namespace std;
- int main()
- {
- TCHAR szBuffer[10] = {
- TEXT('A'),TEXT('A'),TEXT('A'),TEXT('A'),TEXT('A'),
- TEXT('A'),TEXT('A'),TEXT('A'),TEXT('A'),' '
- };
- LCID local = GetThreadLocale();
- int result = CompareString(local/*LOCALE_SYSTEM_DEFAULT*/,NORM_IGNORECASE,szBuffer,_countof(szBuffer),TEXT("AAAAAAAAA"),10);
- switch(result)
- {
- case 0:
- cout<<"Error"<<endl;
- break;
- case CSTR_LESS_THAN:
- cout<<"Str1 > Str2"<<endl;
- break;
- case CSTR_GREATER_THAN:
- cout<<"Str1 < Str2"<<endl;
- break;
- case CSTR_EQUAL:
- cout<<"Str1 = Str2"<<endl;
- break;
- default:
- cout<<"Don't goto there"<<endl;
- break;
- }
- cout<<result<<endl;
- return 0;
- }
附录:
Compares two Unicode strings to test binary equivalence.
Syntax
- int CompareStringOrdinal(
- __in LPCWSTR lpString1,
- __in int cchCount1,
- __in LPCWSTR lpString2,
- __in int cchCount2,
- __in BOOL bIgnoreCase
- );
Parameters
- lpString1 [in]
-
Pointer to the first string to compare.
- cchCount1 [in]
-
Length of the string indicated by lpString1. The application supplies -1 if the string is null-terminated. In this case, the function determines the length automatically.
- lpString2 [in]
-
Pointer to the second string to compare.
- cchCount2 [in]
-
Length of the string indicated by lpString2. The application supplies -1 if the string is null-terminated. In this case, the function determines the length automatically.
- bIgnoreCase [in]
-
TRUE if the function is to perform a case-insensitive comparison, using the operating system uppercase table information. The application sets this parameter to FALSE if the function is to compare the strings exactly as they are passed in.
Return Value
Returns one of the following values if successful. To maintain the C runtime convention of comparing strings, the value 2 can be subtracted from a nonzero return value. Then, the meaning of <0, ==0, and >0 is consistent with the C runtime.
- CSTR_LESS_THAN. The value indicated by lpString1 is less than the value indicated bylpString2.
- CSTR_EQUAL. The value indicated by lpString1 equals the value indicated bylpString2.
- CSTR_GREATER_THAN. The value indicated by lpString1 is greater than the value indicated bylpString2.
The function returns 0 if it does not succeed. To get extended error information, the application can callGetLastError, which can return one of the following error codes:
- ERROR_INVALID_PARAMETER. Any of the parameter values was invalid.