温馨提示×

c++中如何求字符串长度

c++
小亿
206
2024-03-28 10:23:11
栏目: 编程语言
C++开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C++中,可以使用std::string类的size()方法或者length()方法来获取字符串的长度。示例代码如下:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    int length1 = str.size();
    int length2 = str.length();
    
    std::cout << "Length of the string is: " << length1 << std::endl;
    std::cout << "Length of the string is: " << length2 << std::endl;
    
    return 0;
}

以上代码会输出字符串Hello, World!的长度,即13

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:C++中怎么用sizeof求字符串长度

0