strcoll 函数strcoll Functions

每个 strcollwcscoll 函数根据当前使用的区域设置代码页的 LC_COLLATE 类别设置来比较两个字符串。Each of the strcoll and wcscoll functions compares two strings according to the LC_COLLATE category setting of the locale code page currently in use. 每个 _mbscoll 函数根据当前使用的多字节代码页比较两个字符串。Each of the _mbscoll functions compares two strings according to the multibyte code page currently in use. 在以下情况下使用 coll 函数进行字符串比较:当前代码页中的字符集顺序和字典字符顺序存在差异,以及此差异对于比较很有用。Use the coll functions for string comparisons when there is a difference between the character set order and the lexicographic character order in the current code page and this difference is of interest for the comparison. 使用相应的 cmp 函数仅测试字符串相等性。Use the corresponding cmp functions to test only for string equality.

strcoll 函数strcoll Functions

SBCSSBCS UnicodeUnicode MBCSMBCS 描述Description
strcollstrcoll wcscollwcscoll _mbscoll_mbscoll 排序两个字符串Collate two strings
_stricoll_stricoll _wcsicoll_wcsicoll _mbsicoll_mbsicoll 排序两个字符串(不区分大小写)Collate two strings (case insensitive)
_strncoll_strncoll _wcsncoll_wcsncoll _mbsncoll_mbsncoll 排序两个字符串的第一个 count 字符Collate first count characters of two strings
_strnicoll_strnicoll _wcsnicoll_wcsnicoll _mbsnicoll_mbsnicoll 排序两个字符串的第一个 count 字符(不区分大小写)Collate first count characters of two strings (case-insensitive)

备注Remarks

这些函数(strcollstricoll_strncoll_strnicoll)的单字节字符 (SBCS) 版本根据当前区域设置的 LC_COLLATE 类别设置比较 string1string2The single-byte character (SBCS) versions of these functions (strcoll, stricoll, _strncoll, and _strnicoll) compare string1 and string2 according to the LC_COLLATE category setting of the current locale. 这些函数与相应的 strcmp 函数不同,因为 strcoll 函数使用提供排序序列的区域设置代码页信息。These functions differ from the corresponding strcmp functions in that the strcoll functions use locale code page information that provides collating sequences. 对于在字符集顺序和字典字符顺序不同的区域设置中比较字符串,应使用 strcoll 函数而不是相应的 strcmp 函数。For string comparisons in locales in which the character set order and the lexicographic character order differ, the strcoll functions should be used rather than the corresponding strcmp functions. 有关 LC_COLLATE 的详细信息,请参阅 setlocaleFor more information on LC_COLLATE, see setlocale.

对于一些代码页和相应的字符集,字符集中的字符顺序可能与字典的字符顺序不同。For some code pages and corresponding character sets, the order of characters in the character set may differ from the lexicographic character order. 在“C”区域设置中并非如此:ASCII 字符集中的字符顺序与字典的字符顺序相同。In the "C" locale, this is not the case: the order of characters in the ASCII character set is the same as the lexicographic order of the characters. 但是,在某些欧洲代码页中,例如,字符“a”(值 0x61)位于字符“ä”(值 0xE4)之前,但在字典顺序中,字符“ä”位于字符“a”之前。However, in certain European code pages, for example, the character 'a' (value 0x61) precedes the character 'ä' (value 0xE4) in the character set, but the character 'ä' precedes the character 'a' lexicographically. 要在这种实例中执行字典比较,请使用 strcoll 而不是 strcmpTo perform a lexicographic comparison in such an instance, use strcoll rather than strcmp. 或者,也可以对原始字符串使用 strxfrm,然后对结果字符串使用 strcmpAlternatively, you can use strxfrm on the original strings, then use strcmp on the resulting strings.

strcollstricoll_strncoll_strnicoll 根据当前使用的区域设置代码页自动处理多字节字符串,如同处理其对应的宽字符 (Unicode) 部分一样。strcoll, stricoll, _strncoll, and _strnicoll automatically handle multibyte-character strings according to the locale code page currently in use, as do their wide-character (Unicode) counterparts. 然而,这些函数的多字节 (MBCS) 版本根据当前使用的多字节代码页以字符为基础排序字符串。The multibyte-character (MBCS) versions of these functions, however, collate strings on a character basis according to the multibyte code page currently in use.

由于 coll 函数按字典顺序整理字符串以进行比较,而 cmp 函数仅测试字符串对等性,因此 coll 函数速度比相应的 cmp 版本慢得多。Because the coll functions collate strings lexicographically for comparison, whereas the cmp functions simply test for string equality, the coll functions are much slower than the corresponding cmp versions. 因此,应仅在以下情况下使用 coll 函数:当前代码页中的字符集顺序和字典字符顺序存在差异,以及此差异对于字符串比较很有用。Therefore, the coll functions should be used only when there is a difference between the character set order and the lexicographic character order in the current code page and this difference is of interest for the string comparison.

另请参阅See Also

区域设置 Locale
字符串操作 String Manipulation
localeconv localeconv
_mbsnbcoll、_mbsnbcoll_l、_mbsnbicoll、_mbsnbicoll_l _mbsnbcoll, _mbsnbcoll_l, _mbsnbicoll, _mbsnbicoll_l
setlocale、_wsetlocale setlocale, _wsetlocale
strcmp、wcscmp、_mbscmp strcmp, wcscmp, _mbscmp
strncmp、wcsncmp、_mbsncmp、_mbsncmp_l strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
_strnicmp、_wcsnicmp、_mbsnicmp、_strnicmp_l、_wcsnicmp_l、_mbsnicmp_l _strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strxfrm、wcsxfrm、_strxfrm_l、_wcsxfrm_lstrxfrm, wcsxfrm, _strxfrm_l, _wcsxfrm_l