名前空間
変種
操作

std::strlen

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

指定されたバイト文字列の長さを返します。これは、strが指す文字配列内の文字数で、最初のnull文字までは含まれません。 strが指す文字配列内にnull文字がない場合、動作は未定義です。

目次

[編集] パラメータ

str - 調査対象のnull終端バイト文字列へのポインタ

[編集] 戻り値

null終端文字列 str の長さ。

[編集] 可能な実装

std::size_t strlen(const char* start)
{
    // NB: start is not checked for nullptr!
    const char* end = start;
    while (*end != '\0')
        ++end;
    return end - start;
}

[編集]

#include <cstring>
#include <iostream>
 
int main()
{
    const char str[] = "dog cat\0mouse";
 
    std::cout << "without null character: " << std::strlen(str) << '\n'
              << "with null character: " << sizeof str << '\n';
}

出力

without null character: 7
with null character: 14

[編集] 関連項目

ワイド文字列の長さを返す
(関数) [編集]
次のマルチバイト文字のバイト数を返す
(関数) [編集]
Cドキュメント (strlenについて)
English 日本語 中文(简体) 中文(繁體)