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 は文字列を 1 バイト文字列として扱うため、マルチバイト文字が含まれている場合でも、戻り値は常にバイト数と同じです。 wcslenstrlen のワイド文字バージョンです。wcslen の引数はワイド文字列で、文字数はワイド (2 バイト) 文字の単位です。 それ以外では、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 を返し、errnoEILSEQ に設定します。

出力値は、ロケールの LC_CTYPE カテゴリ設定の設定によって影響を受けます。 詳細については、setlocaleを参照してください。 _l サフィックスが付いていないこれらの関数のバージョンでは、このロケールに依存する動作に現在のロケールを使用します。_l サフィックスが付いているバージョンは、渡されたロケール パラメーターを代わりに使用する点を除いて同じです。 詳細については、「 Locale」を参照してください。

必要条件

ルーチンによって返される値 必須ヘッダー
strlen <string.h>
wcslen <string.h> または <wchar.h>
_mbslen, _mbslen_l <mbstring.h>
_mbstrlen, _mbstrlen_l <stdlib.h>

互換性の詳細については、「 Compatibility」を参照してください。

// 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
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