C++的string类属于标准库中的std命名空间,封装了字符串的操作逻辑,不需要开发者手动管理内存,避免了传统C风格字符串容易出现的内存溢出、越界访问等问题。它提供了丰富的成员函数,覆盖大部分常见字符串处理场景,是C++开发中最常用的字符串处理工具之一。
string类的初始化方法
string类支持多种初始化方式,开发者可以根据实际需求选择合适的初始化形式。
#include <iostream>
#include <string>
using namespace std;
int main() {
// 默认初始化,为空字符串
string s1;
// 用C风格字符串初始化
string s2 = "hello";
// 用另一个string对象初始化
string s3 = s2;
// 初始化为指定数量的相同字符
string s4(5, 'a');
// 用部分C风格字符串初始化,取前3个字符
string s5("world", 3);
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
cout << "s3: " << s3 << endl;
cout << "s4: " << s4 << endl;
cout << "s5: " << s5 << endl;
return 0;
}
常用字符串拼接操作
string类支持多种方式拼接字符串,比C风格字符串的拼接更简洁安全。
使用+运算符拼接
可以直接用+运算符拼接两个string对象,或者string对象和C风格字符串、字符。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "hello";
string s2 = "world";
// 拼接两个string对象
string s3 = s1 + s2;
// 拼接string和C风格字符串
string s4 = s1 + " ";
// 拼接string和字符
string s5 = s4 + '!';
cout << s3 << endl; // 输出helloworld
cout << s5 << endl; // 输出hello !
return 0;
}
使用append成员函数拼接
append函数可以在原字符串末尾追加内容,支持追加指定长度的字符串。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello";
// 追加C风格字符串
s.append(" world");
// 追加另一个string对象
string suffix = "!!!";
s.append(suffix);
// 追加指定长度的C风格字符串,取前3个字符
s.append("abcdef", 3);
cout << s << endl; // 输出hello world!!!abc
return 0;
}
字符串查找操作
string类提供了多个查找函数,返回查找结果的索引位置,如果未找到则返回string::npos。
| 函数名 | 功能说明 |
|---|---|
| find(str) | 查找str第一次出现的位置 |
| rfind(str) | 查找str最后一次出现的位置 |
| find_first_of(str) | 查找str中任意字符第一次出现的位置 |
| find_last_of(str) | 查找str中任意字符最后一次出现的位置 |
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world hello";
// 查找第一次出现hello的位置
size_t pos1 = s.find("hello");
// 查找最后一次出现hello的位置
size_t pos2 = s.rfind("hello");
// 查找第一个元音字母的位置
size_t pos3 = s.find_first_of("aeiou");
// 判断是否找到结果
if (pos1 != string::npos) {
cout << "第一次找到hello的位置:" << pos1 << endl;
}
if (pos2 != string::npos) {
cout << "最后一次找到hello的位置:" << pos2 << endl;
}
if (pos3 != string::npos) {
cout << "第一个元音字母的位置:" << pos3 << endl;
}
return 0;
}
字符串截取与修改操作
substr截取子串
substr函数可以从指定位置开始截取指定长度的子字符串,第一个参数是起始索引,第二个参数是截取长度,如果不传第二个参数则截取到字符串末尾。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world";
// 从索引0开始截取5个字符
string sub1 = s.substr(0, 5);
// 从索引6开始截取到末尾
string sub2 = s.substr(6);
cout << "sub1: " << sub1 << endl; // 输出hello
cout << "sub2: " << sub2 << endl; // 输出world
return 0;
}
erase删除字符
erase函数可以删除指定位置、指定长度的字符,也可以删除指定迭代器范围的字符。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world";
// 删除从索引5开始的1个字符(空格)
s.erase(5, 1);
cout << "删除后:" << s << endl; // 输出helloworld
// 删除最后一个字符
s.erase(s.size() - 1);
cout << "再删除后:" << s << endl; // 输出helloworl
return 0;
}
replace替换字符
replace函数可以替换指定位置、指定长度的字符为新的内容。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello world";
// 替换从索引6开始的5个字符为cpp
s.replace(6, 5, "cpp");
cout << "替换后:" << s << endl; // 输出hello cpp
return 0;
}
字符串比较操作
string类重载了==、!=、<、>等比较运算符,也提供了compare成员函数进行更灵活的字符串比较。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "apple";
string s2 = "banana";
// 使用比较运算符
if (s1 < s2) {
cout << s1 << " 小于 " << s2 << endl;
}
// 使用compare函数,返回0表示相等,小于0表示当前字符串更小,大于0表示当前字符串更大
int ret = s1.compare(s2);
if (ret < 0) {
cout << "compare结果:" << s1 << " 小于 " << s2 << endl;
}
return 0;
}
其他常用操作
- size()和length():返回字符串的长度,两个函数功能完全一致。
- empty():判断字符串是否为空,为空返回true,否则返回false。
- c_str():返回对应的C风格字符串指针,方便和需要C风格字符串的接口对接。
- clear():清空字符串内容。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "test";
cout << "长度:" << s.size() << endl; // 输出4
cout << "是否为空:" << (s.empty() ? "是" : "否") << endl; // 输出否
// 获取C风格字符串
const char* c_s = s.c_str();
cout << "C风格字符串:" << c_s << endl;
s.clear();
cout << "清空后是否为空:" << (s.empty() ? "是" : "否") << endl; // 输出是
return 0;
}