strxfrm
來自 cppreference.com
定義於標頭檔案 <string.h> |
||
(直到 C99) | ||
(C99 起) | ||
將 src
所指向的以空字元結尾的位元組字串轉換為由實現定義的格式,使得使用 strcmp 比較兩個轉換後的字串與使用 strcoll 比較原始字串在當前 C 區域設定下產生相同的結果。
轉換後的字串的前 count
個字元(包括終止空字元)寫入目標位置,並返回完整轉換後的字串的長度(不包括終止空字元)。
如果 dest
陣列不夠大,則行為未定義。如果 dest
和 src
重疊,則行為未定義。
如果 count
為 0,則允許 dest
為空指標。
目錄 |
[編輯] 註釋
可以接收整個轉換後的字串的緩衝區的正確長度為 1+strxfrm(NULL, src, 0)
此函式用於使用相同字串或一組字串進行多次依賴於區域設定的比較時,因為使用 strxfrm
只轉換所有字串一次,然後使用 strcmp 比較轉換後的字串更有效率。
[編輯] 引數
dest | - | 指向將寫入轉換後字串的陣列的第一個元素的指標 |
src | - | 指向要轉換的以空字元結尾的位元組字串的第一個字元的指標 |
count | - | 要寫入的最大字元數 |
[編輯] 返回值
轉換後的字串的長度,不包括終止空字元。
[編輯] 示例
執行此程式碼
#include <stdio.h> #include <string.h> #include <locale.h> int main(void) { setlocale(LC_COLLATE, "cs_CZ.iso88592"); const char *in1 = "hrnec"; char out1[1+strxfrm(NULL, in1, 0)]; strxfrm(out1, in1, sizeof out1); const char *in2 = "chrt"; char out2[1+strxfrm(NULL, in2, 0)]; strxfrm(out2, in2, sizeof out2); printf("In the Czech locale: "); if(strcmp(out1, out2) < 0) printf("%s before %s\n",in1, in2); else printf("%s before %s\n",in2, in1); printf("In lexicographical comparison: "); if(strcmp(in1, in2)<0) printf("%s before %s\n",in1, in2); else printf("%s before %s\n",in2, in1); }
可能的輸出
In the Czech locale: hrnec before chrt In lexicographical comparison: chrt before hrnec
[編輯] 參考
- C17 標準 (ISO/IEC 9899:2018)
- 7.24.4.5 strxfrm 函式 (p: 267)
- C11 標準 (ISO/IEC 9899:2011)
- 7.24.4.5 strxfrm 函式 (p: 366-367)
- C99 標準 (ISO/IEC 9899:1999)
- 7.21.4.5 strxfrm 函式 (p: 329-330)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 4.11.4.5 strxfrm 函式
[編輯] 另請參閱
根據當前區域設定比較兩個字串 (函式) | |
(C95) |
根據當前語言環境比較兩個寬字串 (函式) |
比較兩個字串 (函式) | |
(C95) |
轉換寬字串,使 wcscmp 產生與 wcscoll 相同的結果 (函式) |
C++ 文件 關於 strxfrm
|