名稱空間
變體
操作

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