strnlen, strnlen_s, wcsnlen, wcsnlen_s, _mbsnlen, _mbsnlen_l, _mbstrnlen, _mbstrnlen_l

使用当前区域设置或已传入的一个区域设置获取字符串的长度。 这些函数是 strlenwcslen_mbslen_mbslen_l_mbstrlen_mbstrlen_l 的更安全的版本。

重要

_mbsnlen_mbsnlen_l_mbstrnlen_mbstrnlen_l 无法用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数

语法

size_t strnlen(
   const char *str,
   size_t numberOfElements
);
size_t strnlen_s(
   const char *str,
   size_t numberOfElements
);
size_t wcsnlen(
   const wchar_t *str,
   size_t numberOfElements
);
size_t wcsnlen_s(
   const wchar_t *str,
   size_t numberOfElements
);
size_t _mbsnlen(
   const unsigned char *str,
   size_t numberOfElements
);
size_t _mbsnlen_l(
   const unsigned char *str,
   size_t numberOfElements,
   _locale_t locale
);
size_t _mbstrnlen(
   const char *str,
   size_t numberOfElements
);
size_t _mbstrnlen_l(
   const char *str,
   size_t numberOfElements,
   _locale_t locale
);

参数

str
以 Null 结尾的字符串。

numberOfElements
字符串缓冲区的大小。

locale
要使用的区域设置。

返回值

这些函数将返回字符串中的字符数,不包括以 null 结尾的字符。 如果字符串的第一个 numberOfElements 字节中不包含 null 终止符(或 wcsnlen 的宽字符),则返回 numberOfElements 以指示错误条件;以 null 结尾的字符串的长度严格小于 numberOfElements

如果字符串包含无效的多字节字符,则 _mbstrnlen_mbstrnlen_l 将返回 -1。

备注

注意

strnlen 不是 strlen 的替代方法;应将 strnlen 仅用于计算已知大小的缓冲区中传入的不受信任数据的大小,例如网络数据包。 strnlen 计算长度,但是如果该字符串是非终止的,则它不会超过缓冲区的末尾。 对于其他情况,请使用 strlen。 (同一情况适用于 wcsnlen_mbsnlen_mbstrnlen。)

其中每个函数都将返回 str 中的字符数,不包括以 null 结尾的字符。 但是,strnlenstrnlen_s 会将字符串解释为单字节字符字符串,因此即使该字符串包含多字节字符,返回值也始终等于字节数。 wcsnlenwcsnlen_s 分别是 strnlenstrnlen_s 的宽字符版本;wcsnlenwcsnlen_s 的参数是宽字符字符串且字符计数以宽字符为单位。 除此以外,wcsnlenstrnlen 的行为完全相同,strnlen_swcsnlen_s 也是如此。

strnlenwcsnlen_mbsnlen 不会验证其参数。 如果 strNULL,则会发生访问冲突。

strnlen_swcsnlen_s 将验证其参数。 如果 strNULL,则函数返回 0。

_mbstrnlen 也会验证其参数。 如果 strNULL,或者如果 numberOfElements 大于 INT_MAX,则 _mbstrnlen 将生成无效的参数异常,如参数验证中所述。 如果允许执行继续,则 _mbstrnlen 会将 errno 设置为 EINVAL 并返回 -1。

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcsnlen strnlen strnlen wcsnlen
_tcscnlen strnlen _mbsnlen wcsnlen
_tcscnlen_l strnlen _mbsnlen_l wcsnlen

_mbsnlen_mbstrnlen 以多字节字符字符串格式返回多字节字符的数量。 _mbsnlen 根据当前正在使用的多字节代码页或已传入的区域设置来识别多字节字符序列;它不会测试多字节字符的有效性。 _mbstrnlen 将测试多字节字符的有效性并识别多字节字符序列。 如果传递给 _mbstrnlen 的字符串包含无效的多字节字符,则 errno 将设置为 EILSEQ

输出值受区域设置的 LC_CTYPE 类别设置的影响。 有关详细信息,请参阅 setlocale。 这些函数的版本相同,差异在于不包含 _l 后缀的版本使用与此区域设置相关的行为的当前区域设置,而包含 _l 后缀的版本则使用传入的区域设置参数。 有关详细信息,请参阅 Locale

要求

例程 必需的标头
strnlenstrnlen_s <string.h>
wcsnlenwcsnlen_s <string.h><wchar.h>
_mbsnlen_mbsnlen_l <mbstring.h>
_mbstrnlen_mbstrnlen_l <stdlib.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_strnlen.c

#include <string.h>

int main()
{
   // str1 is 82 characters long. str2 is 159 characters long

   char* str1 = "The length of a string is the number of characters\n"
               "excluding the terminating null.";
   char* str2 = "strnlen takes a maximum size. If the string is longer\n"
                "than the maximum size specified, the maximum size is\n"
                "returned rather than the actual size of the string.";
   size_t len;
   size_t maxsize = 100;

   len = strnlen(str1, maxsize);
   printf("%s\n Length: %d \n\n", str1, len);

   len = strnlen(str2, maxsize);
   printf("%s\n Length: %d \n", str2, len);
}
The length of a string is the number of characters
excluding the terminating null.
Length: 82

strnlen takes a maximum size. If the string is longer
than the maximum size specified, the maximum size is
returned rather than the actual size of the string.
Length: 100

另请参阅

字符串操作
区域设置
多字节字符序列的解释
setlocale_wsetlocale
strncat_strncat_lwcsncat_wcsncat_l_mbsncat_mbsncat_l
strncmpwcsncmp_mbsncmp_mbsncmp_l
strcoll 函数
strncpy_s_strncpy_s_lwcsncpy_s_wcsncpy_s_l_mbsncpy_s_mbsncpy_s_l
strrchrwcsrchr_mbsrchr_mbsrchr_l
_strset_strset_l_wcsset_wcsset_l_mbsset_mbsset_l
strspnwcsspn_mbsspn_mbsspn_l