自学内容网 自学内容网

C++学习,标准库 <string> 成员函数

C++ 标准库(Standard Template Library, STL)是 C++ 的核心组成部分之一,提供了丰富的数据结构和算法。C++标准库中的<string>是专门用于处理字符串的头文件。它提供了std::string类,该类是对C风格字符串的封装,并提供了更安全、更易用的字符串操作功能。

std::string 成员函数:

函数名描述示例代码
size()返回字符串的长度(字符数)。std::cout << str.size();
length()与 size() 相同,返回字符串的长度。std::cout << str.length();
empty()判断字符串是否为空。std::cout << (str.empty() ? "Yes" : "No");
operator[]访问字符串中指定位置的字符。std::cout << str[0];
at()访问字符串中指定位置的字符(带边界检查)。std::cout << str.at(0);
substr()返回从指定位置开始的子字符串。std::string sub = str.substr(0, 5);
find()查找子字符串在字符串中的位置。std::cout << str.find("sub") << std::endl;
rfind()从字符串末尾开始查找子字符串的位置。std::cout << str.rfind("sub") << std::endl;
replace()替换字符串中的部分内容。str.replace(pos, length, "new_substring");
append()在字符串末尾添加内容。str.append(" more");
insert()在指定位置插入内容。str.insert(pos, "inserted");
erase()删除指定位置的字符或子字符串。str.erase(pos, length);
clear()清空字符串。str.clear();
c_str()返回 C 风格的字符串(以 null 结尾)。const char* cstr = str.c_str();
data()返回指向字符数据的指针(C++11 及之后的版本)。const char* data = str.data();
compare()比较两个字符串。int result = str.compare("other");
find_first_of()查找第一个匹配任意字符的位置。size_t pos = str.find_first_of("aeiou");
find_last_of()查找最后一个匹配任意字符的位置。size_t pos = str.find_last_of("aeiou");
find_first_not_of()查找第一个不匹配任意字符的位置。size_t pos = str.find_first_not_of("aeiou");
find_last_not_of()查找最后一个不匹配任意字符的位置。size_t pos = str.find_last_not_of("aeiou");

 

 


原文地址:https://blog.csdn.net/xuann/article/details/143063560

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!