現在のロケールまたは指定されたロケールを使用し、文字列の長さを取得します。 これらの関数のセキュリティを強化したバージョンを使用できます。「strnlen、strnlen_s、wcsnlen、wcsnlen_s、_mbsnlen、_mbsnlen_l、_mbstrnlen、_mbstrnlen_l」を参照してください。
構文
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 バイト文字列として扱うため、マルチバイト文字が含まれている場合でも、戻り値は常にバイト数と同じです。 wcslen は strlen のワイド文字バージョンです。wcslen の引数はワイド文字列で、文字数はワイド (2 バイト) 文字の単位です。 それ以外では、wcslen と strlen の動作は同じです。
セキュリティに関するメモ これらの関数は、バッファー オーバーランが原因で発生する潜在的な脅威の影響を受けます。 バッファー オーバーランは、システムを攻撃するときによく使用される方法であり、その結果、認められていない権限が昇格されます。 詳しくは、「 バッファー オーバーランの回避」をご覧ください。
既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 これを変更するには、「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’ : %dn”, str1, strlen(str1) ); // wcslen gives the length of a wide character string wprintf(L”Length of ‘%s’ : %dn”, wstr1, wcslen(wstr1) ); // A multibyte string: [A] [B] [C] [katakana A] [D] [] // in Code Page 932. For this example to work correctly, // the Japanese language support must be enabled by the // operating system. mbstr1 = “ABC” “x83x40” “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 %sn”, locale_string); } // _mbslen will recognize the Japanese multibyte character if the // current locale used by the operating system is Japanese printf(“Length of ‘%s’ : %dn”, 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’ : %dn”, mbstr1, _mbstrlen(mbstr1) ); printf(“Bytes in ‘%s’ : %dn”, 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
Trả lời