名前空間
変種
操作

std::strlen

提供: cppreference.com
< cpp‎ | string‎ | byte
ヘッダ <cstring> で定義
std::size_t strlen( const char* str );

指定されたバイト文字列の長さ、つまり、 str によって最初の要素が指されている文字配列内の、最初のヌル文字直前までの文字数を返します。 str の指す文字配列にヌル文字がない場合、動作は未定義です。

目次

[編集] 引数

str - 調べるヌル終端バイト文字列を指すポインタ

[編集] 戻り値

ヌル終端文字列 str の長さ。

[編集] 実装例

std::size_t strlen(const char* start) {
   const char* end = start;
   while(*end++ != 0);
   return end - start - 1;
}

[編集]

#include <cstring>
#include <iostream>
 
int main()
{
   const char str[] = "How many characters does this string contain?";
 
   std::cout << "without null character: " << std::strlen(str) << '\n'
             << "with null character: " << sizeof str << '\n';
}

出力:

without null character: 45
with null character: 46

[編集] 関連項目

ワイド文字列の長さを返します
(関数) [edit]
次のマルチバイト文字のバイト数を返します
(関数) [edit]