名稱空間
變體
操作

wcslen, wcsnlen_s

來自 cppreference.com
< c‎ | string‎ | wide
在標頭檔案 <wchar.h> 中定義
size_t wcslen( const wchar_t *str );
(1) (自 C95 起)
size_t wcsnlen_s(const wchar_t *str, size_t strsz);
(2) (C11 起)
1) 返回寬字串的長度,即終止空寬字元之前非空寬字元的數量。
2)(1) 相同,但如果 str 是空指標則返回零,如果在前 strsz 個寬字元中未找到空寬字元則返回 strsz
與所有邊界檢查函式一樣,只有當實現定義了 __STDC_LIB_EXT1__ 且使用者在包含 <stdio.h> 之前將 __STDC_WANT_LIB_EXT1__ 定義為整數常量 1 時,才能保證 wcslen_s 可用。

目錄

[編輯] 引數

str - 指向要檢查的以空字元終止的寬字串的指標
strsz - 要檢查的最大寬字元數

[編輯] 返回值

1) 以空字元終止的寬字串 str 的長度。
2) 成功時返回以空字元終止的寬字串 str 的長度,如果 str 是空指標則返回零,如果未找到空寬字元則返回 strsz

[編輯] 注意

strnlen_swcsnlen_s 是唯一不呼叫執行時約束處理程式的邊界檢查函式。它們是純實用函式,用於為非空終止字串提供有限支援。

[編輯] 示例

#include <wchar.h>
#include <stdio.h>
 
int main(void)
{
    wchar_t str[] = L"How many wide characters does this string contain?";
 
    printf("without null character: %zu\n", wcslen(str));
    printf("with null character: %zu\n", sizeof str / sizeof *str);
}

輸出

without null character: 50
with null character: 51

[編輯] 參考

  • C11 標準 (ISO/IEC 9899:2011)
  • 7.29.4.6.1 wcslen 函式 (p: 439)
  • K.3.9.2.4.1 wcsnlen_s 函式 (p: 646-647)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.24.4.6.1 wcslen 函式 (p: 385)

[編輯] 另請參閱

返回給定字串的長度
(函式) [編輯]
C++ 文件 for wcslen