在C++中,string是一个功能强大的类,用于处理和操作文本数据。它属于C++标准库中的字符串库部分,专门用于处理字符串。与传统的C风格字符串相比,它提供了动态内存管理、类型安全和丰富的操作方法。

目录
一、构造和初始化
二、获取字符串内容
三、查找字符串位置
四、修改字符串
1. 删除
2.插入
3.连接
4.替换
五、字符串长度和大小
一、构造和初始化
- string s:使用默认构造函数创建一个空字符串。
- string s(const char* s):用C风格的字符串构造。
- string s(size_t count, char ch):从给定数量的重复字符构造。
- 复制构造
- string s(const string& other):从另一个字符串的子串构造
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1; // 默认构造,创建空字符串
string s2("Hello"); // 从C风格字符串构造
string s2_1("Hello",3); //前三个字符
string s2_2("Hello", 2, 3); //从第三个开始,长度为三
string s3(10, 'x'); // 从10个字符'x'构造
string s4 = s2; // 将s2复制给s4
string s5(s2, 3); //从s2的第四个字符到最后
string s5_1(s2, 2, 2); 从s2的第三个字符开始,长度为2
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
cout << "s2_1: " << s2_1 << endl;
cout << "s2_2: " << s2_2 << endl;
cout << "s3: " << s3 << endl;
cout << "s4: " << s4 << endl;
cout << "s5: " << s5 << endl;
cout << "s5_1: " << s5_1 << endl;
return 0;
}
二、获取字符串内容
- 通过下标操作符访问
substr(size_t pos = 0, size_t len = npos):返回从pos开始的、长度为len的子字符串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s("Hello World");
char c = s[0]; // 'H'
string s1 = s.substr(6,5); //获取从第7个开始,长度为5的字符串
cout << "c: " << c << endl;
cout << "s1: " << s1 << endl;
return 0;
}

三、查找字符串位置
find(const string& str, size_t pos = 0):查找子字符串str,从位置pos开始搜索rfind(const string& str, size_t pos = npos):从字符串末尾开始,反向查找子字符串str
#include <iostream>
#include <string>
using namespace std;
int main() {
string s("Hello World");
int pos1 = s.find("World"); // 返回子字符串"World"的起始位置
int pos2_1 = s.rfind("World"); //结尾开始反向查找
int pos2_2 = s.rfind("World", 9); //第10个开始,反向查找
cout << "pos1: " << pos1 << endl;
cout << "pos2_1: " << pos2_1 << endl;
cout << "pos2_2: " << pos2_2 << endl;
return 0;
}
四、修改字符串
1. 删除
erase(size_t index, size_t count):删除从index开始的count个字符。
2.插入
insert(size_t index, const string& str):在index位置插入字符串str
3.连接
append(const string& str):在字符串末尾拼接上str。- 使用
+或+=运算符连接字符串
4.替换
replace(size_t pos, size_t len, const string& str):替换从pos开始的len个字符为str
#include <iostream>
#include <string>
using namespace std;
int main() {
string s("Hello World");
cout << "s: " << s << endl;
s.erase(6, 5); // 删除从位置6开始的5个字符
cout << "s: " << s << endl;
s.insert(6, "there"); // 在位置6插入"there"
cout << "s: " << s << endl;
s.append("!"); // 在字符串末尾追加"!"
cout << "s: " << s << endl;
s += s; //追加s
cout << "s: " << s << endl;
s.replace(6, 5, "place"); // 替换从位置6开始的5个字符为"place"
cout << "s: " << s << endl;
return 0;
}

五、字符串长度和大小
size()获取字符串的长度length()获取字符串的长度empty():检查字符串是否为空,如果为空,返回true
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World";
cout << "Length1: " << str.size() << endl;
cout << "Length2: " << str.length() << endl;
if (!str.empty()) {
cout << "The string is empty." << endl;
}
return 0;
}

感谢阅读,欢迎大家指正错误。
创作不易,如果我的文章对你有帮助,请点赞、收藏和关注,您的支持是我前进的最大动力。




















![[数据集][目标检测]生产线上金属罐易拉罐正反面检测数据集VOC+YOLO格式2715张2类别](https://i-blog.csdnimg.cn/direct/afe05e380f174efa885aa38c732498f3.png)