strlen、wcslen、_mbslen、_mbslen_l、_mbstrlen、_mbstrlen_l

通过使用当前区域设置或指定区域设置获取字符串的长度。 提供这些函数的更安全版本;请参阅 strnlen、strnlen_s、wcsnlen、wcsnlen_s、_mbsnlen、_mbsnlen_l、_mbstrnlen、_mbstrnlen_l

重要

_mbslen、_mbslen_l、_mbstrlen 和 _mbstrlen_l 无法用于在 Windows 运行时中执行的应用程序。有关详细信息,请参阅 /ZW 不支持 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 会将字符串解释为单字节字符字符串,因此即使该字符串包含多字节字符,其返回值也始终等于字节数。 wcslen 是 strlen 的宽字符版本;wcslen 的参数是宽字符字符串且字符计数采用宽(双字节)字符。 除此以外,wcslen 和 strlen 的行为完全相同。

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

一般文本例程映射

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 后缀的版本相同,只不过它们使用传递的区域设置参数。 有关详细信息,请参见区域设置

要求

例程

必需的标头

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) );

   // wstrlen 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) );   
  
}
  

.NET Framework 等效项

System::String::Length

请参见

参考

字符串操作 (CRT)

多字节字符序列的解释

区域设置

setlocale、_wsetlocale

strcat、wcscat、_mbscat

strcmp、wcscmp、_mbscmp

strcoll 函数

strcpy、wcscpy、_mbscpy

strrchr、wcsrchr、_mbsrchr、_mbsrchr_l

_strset、_strset_l、_wcsset、_wcsset_l、_mbsset、_mbsset_l

strspn、wcsspn、_mbsspn、_mbsspn_l