Share via


strlen、wcslen、_mbslen、_mbslen_l、_mbstrlen、_mbstrlen_l

使用目前的地區設定或指定的地區設定取得字串的長度。 這些函式已有更安全的版本可用,請參閱 strnlen、strnlen_s、wcsnlen、wcsnlen_s、_mbsnlen、_mbsnlen_l、_mbstrnlen、_mbstrnlen_l

重要

在 Windows 執行階段中執行的應用程式中無法使用 _mbslen、_mbslen_l、_mbstrlen 和 _mbstrlen_l。如需詳細資訊,請參閱 /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