名稱空間
變體
操作

strcoll

來自 cppreference.com
< c‎ | string‎ | byte
定義於標頭檔案 <string.h>
int strcoll( const char* lhs, const char* rhs );

根據 LC_COLLATE 類別定義的當前區域設定,比較兩個以空字元結尾的位元組字串。

目錄

[編輯] 引數

lhs, rhs - 指向要比較的空終止位元組字串的指標

[編輯] 返回值

  • 如果 lhs 小於(在前面)rhs,則返回負值。
  • 如果 lhs 等於 rhs,則返回 0
  • 如果 lhs 大於(在後面)rhs,則返回正值。

[編輯] 注意

排序順序是字典順序:字母在國家字母表中的位置(其等價類)具有比其大小寫或變體更高的優先順序。在等價類中,小寫字元排在其大寫等價物之前,並且特定於區域設定的順序可能適用於帶有變音符號的字元。在某些區域設定中,字元組作為一個單獨的排序單元進行比較。例如,捷克語中的 "ch" 位於 "h" 之後,"i" 之前;匈牙利語中的 "dzs" 位於 "dz" 之後,"g" 之前。

[編輯] 示例

#include <locale.h>
#include <stdio.h>
#include <string.h>
 
int main(void)
{
    setlocale(LC_COLLATE, "cs_CZ.utf8");
    // Alternatively, ISO-8859-2 (a.k.a. Latin-2)
    // may also work on some OS:
    // setlocale(LC_COLLATE, "cs_CZ.iso88592");
 
    const char* s1 = "hrnec";
    const char* s2 = "chrt";
 
    printf("In the Czech locale: ");
    if (strcoll(s1, s2) < 0)
        printf("%s before %s\n", s1, s2);
    else
        printf("%s before %s\n", s2, s1);
 
    printf("In lexicographical comparison: ");
    if (strcmp(s1, s2) < 0)
        printf("%s before %s\n", s1, s2);
    else
        printf("%s before %s\n", s2, s1);
}

輸出

In the Czech locale: hrnec before chrt
In lexicographical comparison: chrt before hrnec

[編輯] 參考

  • C23 標準 (ISO/IEC 9899:2024)
  • 7.24.4.3 The strcoll function (p: 待定)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.24.4.3 The strcoll function (p: 待定)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.24.4.3 The strcoll function (p: 366)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.21.4.3 The strcoll function (p: 329)
  • C89/C90 標準 (ISO/IEC 9899:1990)
  • 4.11.4.3 The strcoll function

[編輯] 另請參閱

根據當前語言環境比較兩個寬字串
(函式) [編輯]
轉換字串,使 strcmp 產生與 strcoll 相同的結果
(函式) [編輯]
轉換寬字串,使 wcscmp 產生與 wcscoll 相同的結果
(函式) [編輯]
比較兩個字串
(函式) [編輯]
C++ documentation for strcoll