length()
#include<iostream>
using namespace std;
int main() {
string str = "qwert";
cout << str.length() << endl; //输出:5
system("pause");
return 0;
}
size()
#include<iostream>
using namespace std;
int main() {
string str = "qwert";
cout << str.size() << endl; //输出:5
system("pause");
return 0;
}
strlen()
strlen() 函数计算字符串 str 的长度,直到空结束字符,但不包括空结束字符。
函数声明如下:
size_t strlen(const char *str)
注意strlen()函数的参数类型为const char *类型。
#include<iostream>
using namespace std;
int main() {
const char* test;
test = "CSDN";
cout << test << endl; //输出:CSDN
cout << strlen(test) << endl; //输出:4
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main() {
const char* test;
test = "C\0SDN";
cout << test << endl; //输出:C
cout << strlen(test) << endl; //输出:1
system("pause");
return 0;
}