Share via


strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l

通过使用当前区域设置或指定区域设置获取字符串的长度。 这些函数的更安全版本可供使用;请参阅 strnlenstrnlen_swcsnlenwcsnlen_s_mbsnlen_mbsnlen_l_mbstrnlen_mbstrnlen_l

重要

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

语法

size_t strlen(
   const char *str
);
size_t wcslen(
   const wchar_t *str
);
size_t _mbslen(
   const unsigned char *str
);
size_t _mbslen_l(
   const unsigned char *str,
   _locale_t locale
);
size_t _mbstrlen(
   const char *str
);
size_t _mbstrlen_l(
   const char *str,
   _locale_t locale
);

参数

str
以 Null 结尾的字符串。

locale
要使用的区域设置。

返回值

其中每个函数都将返回 str 中的字符数,终止 null 除外。 不会保留返回值(_mbstrlen_mbstrlen_l 除外)以指示错误,如果字符串包含无效的多字节字符,将返回 ((size_t)(-1))

备注

strlen 会将字符串解释为单字节字符字符串,因此即使该字符串包含多字节字符,其返回值也始终等于字节数。 wcslenstrlen 的宽字符版本;wcslen 的参数是宽字符字符串且字符计数采用宽(双字节)字符。 除此以外,wcslenstrlen 的行为完全相同。

安全说明这些函数会引发由缓冲区溢出问题带来的潜在威胁。 缓冲区溢出问题是常见的系统攻击方法,使权限的提升不能确保。 有关详细信息,请参阅避免缓冲区溢出

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

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_tcslen strlen strlen wcslen
_tcsclen strlen _mbslen wcslen
_tcsclen_l strlen _mbslen_l wcslen

_mbslen_mbslen_l 将返回一个多字节字符字符串中的多字节字符数,但它们不会测试多字节字符的有效性。 _mbstrlen_mbstrlen_l 将测试多字节字符的有效性并识别多字节字符序列。 如果传递到 _mbstrlen_mbstrlen_l 的字符串包含代码页中的无效多字节字符,则该函数将返回 -1 并将 errno 设置为 EILSEQ

输出值受区域设置的 LC_CTYPE 类别设置的影响。 有关详细信息,请参阅 setlocale。 这些不带 _l 后缀的函数的版本使用为该区域设置相关的行为的当前区域设置;带有 _l 后缀的版本相同,只不过它们使用传递的区域设置参数。 有关详细信息,请参阅 Locale

要求

例程 必需的标头
strlen <string.h>
wcslen <string.h><wchar.h>
_mbslen_mbslen_l <mbstring.h>
_mbstrlen_mbstrlen_l <stdlib.h>

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

示例

// crt_strlen.c
// Determine the length of a string. For the multi-byte character
// example to work correctly, the Japanese language support for
// non-Unicode programs must be enabled by the operating system.

#include <string.h>
#include <locale.h>

int main()
{
   char* str1 = "Count.";
   wchar_t* wstr1 = L"Count.";
   char * mbstr1;
   char * locale_string;

   // strlen gives the length of single-byte character string
   printf("Length of '%s' : %d\n", str1, strlen(str1) );

   // wcslen gives the length of a wide character string
   wprintf(L"Length of '%s' : %d\n", wstr1, wcslen(wstr1) );

   // A multibyte string: [A] [B] [C] [katakana A] [D] [\0]
   // in Code Page 932. For this example to work correctly,
   // the Japanese language support must be enabled by the
   // operating system.
   mbstr1 = "ABC" "\x83\x40" "D";

   locale_string = setlocale(LC_CTYPE, "Japanese_Japan");

   if (locale_string == NULL)
   {
      printf("Japanese locale not enabled. Exiting.\n");
      exit(1);
   }
   else
   {
      printf("Locale set to %s\n", locale_string);
   }

   // _mbslen will recognize the Japanese multibyte character if the
   // current locale used by the operating system is Japanese
   printf("Length of '%s' : %d\n", mbstr1, _mbslen(mbstr1) );

   // _mbstrlen will recognize the Japanese multibyte character
   // since the CRT locale is set to Japanese even if the OS locale
   // isnot.
   printf("Length of '%s' : %d\n", mbstr1, _mbstrlen(mbstr1) );
   printf("Bytes in '%s' : %d\n", mbstr1, strlen(mbstr1) );

}
Length of 'Count.' : 6
Length of 'Count.' : 6
Length of 'ABCァD' : 5
Length of 'ABCァD' : 5
Bytes in 'ABCァD' : 6

另请参阅

字符串操作
多字节字符序列的解释
区域设置
setlocale_wsetlocale
strcatwcscat_mbscat
strcmpwcscmp_mbscmp
strcoll 函数
strcpywcscpy_mbscpy
strrchrwcsrchr_mbsrchr_mbsrchr_l
_strset_strset_l_wcsset_wcsset_l_mbsset_mbsset_l
strspnwcsspn_mbsspn_mbsspn_l