std:: strlen
From cppreference.net
C++
Text processing library
| Localization library | |||||||||||||||||||||||||
| Regular expressions library (C++11) | |||||||||||||||||||||||||
| Formatting library (C++20) | |||||||||||||||||||||||||
| Null-terminated sequence utilities | |||||||||||||||||||||||||
| Byte strings | |||||||||||||||||||||||||
| Multibyte strings | |||||||||||||||||||||||||
| Wide strings | |||||||||||||||||||||||||
| Primitive numeric conversions | |||||||||||||||||||||||||
|
|||||||||||||||||||||||||
| Text encoding identifications | |||||||||||||||||||||||||
|
|||||||||||||||||||||||||
Null-terminated byte strings
| Functions | ||||||||||||||||||||||||||||||||||||
| Character classification | ||||||||||||||||||||||||||||||||||||
| Character manipulation | ||||||||||||||||||||||||||||||||||||
| Conversions to numeric formats | ||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| String manipulation | ||||||||||||||||||||||||||||||||||||
| String examination | ||||||||||||||||||||||||||||||||||||
| Character array functions | ||||||||||||||||||||||||||||||||||||
| Miscellaneous | ||||||||||||||||||||||||||||||||||||
|
定义于头文件
<cstring>
|
||
|
std::
size_t
strlen
(
const
char
*
str
)
;
|
||
返回给定字节字符串的长度,即从 str 指向的首元素开始,到第一个空字符(不包括该空字符)为止的字符数组中的字符数量。如果 str 指向的字符数组中不存在空字符,则行为未定义。
目录 |
参数
| str | - | 指向待检测的空终止字节字符串的指针 |
返回值
以空字符结尾的字符串 str 的长度。
可能的实现
std::size_t strlen(const char* start) { // 注意:未对start进行空指针检查! 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 documentation
for
strlen
|
|