wcscoll
來自 cppreference.com
在標頭檔案 <wchar.h> 中定義 |
||
int wcscoll( const wchar_t *lhs, const wchar_t *rhs ); |
(自 C95 起) | |
根據當前安裝的語言環境的 LC_COLLATE 類別定義的排序規則,比較兩個空終止寬字串。
目錄 |
[編輯] 引數
lhs, rhs | - | 指向要比較的以空字元結尾的寬字串的指標 |
[編輯] 返回值
如果 lhs
小於(先於)rhs
,則返回負值。
0 如果 lhs
等於 rhs
。
如果 lhs
大於(後於)rhs
,則返回正值。
[編輯] 注意
排序規則是字典順序:字母在國家字母表中的位置(其**等價類**)優先於其大小寫或變體。在等價類中,小寫字元先於其大寫等價字元排序,並且特定於語言環境的順序可能適用於帶有變音符號的字元。在某些語言環境中,字元組作為單個**排序單元**進行比較。例如,捷克語中的 "ch" 在 "h" 之後、"i" 之前排序,匈牙利語中的 "dzs" 在 "dz" 之後、"g" 之前排序。
[編輯] 示例
執行此程式碼
#include <stdio.h> #include <wchar.h> #include <locale.h> void try_compare(const wchar_t* p1, const wchar_t* p2) { if(wcscoll(p1, p2) < 0) printf("%ls before %ls\n", p1, p2); else printf("%ls before %ls\n", p2, p1); } int main(void) { setlocale(LC_ALL, "en_US.utf8"); printf("In the American locale: "); try_compare(L"hrnec", L"chrt"); setlocale(LC_COLLATE, "cs_CZ.utf8"); printf("In the Czech locale: "); try_compare(L"hrnec", L"chrt"); setlocale(LC_COLLATE, "en_US.utf8"); printf("In the American locale: "); try_compare(L"år", L"ängel"); setlocale(LC_COLLATE, "sv_SE.utf8"); printf("In the Swedish locale: "); try_compare(L"år", L"ängel"); }
可能的輸出
In the American locale: chrt before hrnec In the Czech locale: hrnec before chrt In the American locale: ängel before år In the Swedish locale: år before ängel