Namespaces
Variants

wcslen, wcsnlen_s

From cppreference.net
< 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 为空指针则函数返回零;若在 src 的前 strsz 个宽字符中未找到空宽字符,则返回 strsz
与所有边界检查函数相同,仅当实现定义了 __STDC_LIB_EXT1__ 且用户在包含 <stdio.h> 前将 __STDC_WANT_LIB_EXT1__ 定义为整型常量 1 时, wcslen_s 才保证可用。

目录

参数

str - 指向待检测的以空字符结尾的宽字符串的指针
strsz - 待检测的最大宽字符数

返回值

1) 以空字符结尾的宽字符串 str 的长度。
2) 成功时返回以空字符结尾的宽字符串 str 的长度;若 str 为空指针则返回零;若未找到空宽字符则返回 strsz

注释

strnlen_s wcsnlen_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 函数 (页: 439)
  • K.3.9.2.4.1 wcsnlen_s 函数 (页: 646-647)
  • C99 标准 (ISO/IEC 9899:1999):
  • 7.24.4.6.1 wcslen 函数 (页: 385)

参见

返回给定字符串的长度
(函数)
C++ 文档 for wcslen