第八课 字符串处理
字符串是编程中常用的数据类型,C++提供了string类来方便地处理字符串。相比C语言的字符数组,string更加安全易用。
一、string基本使用
#include <iostream>
#include <string> // 需要引入string头文件
using namespace std;
int main() {
string name = "Tom";
string greeting = "Hello";
cout << name << endl; // Tom
cout << greeting << endl; // Hello
return 0;
}
二、字符串常用操作
- length() / size() – 获取字符串长度
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello";
cout << s.length() << endl; // 5
cout << s.size() << endl; // 5(length和size等价)
string empty = "";
cout << empty.length() << endl; // 0
return 0;
}
- 字符串拼接
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "Hello";
string s2 = " World";
// 方式1:+ 运算符
string s3 = s1 + s2;
cout << s3 << endl; // Hello World
// 方式2:+= 追加
s1 += s2;
cout << s1 << endl; // Hello World
// 方式3:append函数
string s4 = "Hello";
s4.append(" C++");
cout << s4 << endl; // Hello C++
return 0;
}
- == 字符串比较
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "abc";
string s2 = "abc";
string s3 = "xyz";
cout << (s1 == s2) << endl; // 1(相等)
cout << (s1 == s3) << endl; // 0(不等)
// compare函数:返回0表示相等
cout << s1.compare(s2) << endl; // 0
cout << s1.compare(s3) << endl; // 负数(s1<s3)
return 0;
}
- find() – 查找子串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello World";
// 查找子串,返回首次出现的起始位置
size_t pos = s.find("World");
cout << pos << endl; // 6
// 查找单个字符
pos = s.find('o');
cout << pos << endl; // 4(第一个o的位置)
// 找不到返回 string::npos(-1)
pos = s.find("Python");
if (pos == string::npos) {
cout << "未找到" << endl;
}
// find_last_find:查找最后一次出现的位置
pos = s.rfind("o");
cout << pos << endl; // 7(最后一个o的位置)
return 0;
}
- substr() – 获取子串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello World";
// 从位置5开始,截取5个字符
string sub1 = s.substr(5, 5);
cout << sub1 << endl; // World
// 从位置6开始,截取到末尾
string sub2 = s.substr(6);
cout << sub2 << endl; // World
return 0;
}
- replace() – 替换子串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello World";
// 从位置0开始,替换5个字符为"Hi"
s.replace(0, 5, "Hi");
cout << s << endl; // Hi World
// 替换所有"Hi"为"Hello"
s = "Hi Hi Hi";
size_t pos = s.find("Hi");
while (pos != string::npos) {
s.replace(pos, 2, "Hello");
pos = s.find("Hi", pos + 5); // 继续查找下一个
}
cout << s << endl; // Hello Hello Hello
return 0;
}
- push_back() / pop_back() – 末尾添加/删除字符
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello";
// 末尾添加字符
s.push_back('!');
cout << s << endl; // Hello!
// 末尾删除字符
s.pop_back();
cout << s << endl; // Hello
return 0;
}
- insert() – 插入字符串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello";
// 在位置5插入字符串
s.insert(5, " World");
cout << s << endl; // Hello World
// 在开头插入
s.insert(0, "Say: ");
cout << s << endl; // Say: Hello World
return 0;
}
- erase() – 删除字符串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello World";
// 从位置5开始删除3个字符
s.erase(5, 3);
cout << s << endl; // Hello rld
// 删除从位置5到末尾
s.erase(5);
cout << s << endl; // Hello
return 0;
}
- 字符串转换
#include <iostream>
#include <string>
using namespace std;
int main() {
// 数字转字符串
int num = 123;
string s1 = to_string(num);
cout << s1 << endl; // "123"
// 字符串转数字
string s2 = "456";
int num2 = stoi(s2); // 转int
double num3 = stod(s2); // 转double
cout << num2 << endl; // 456
cout << num3 << endl; // 456
return 0;
}
三、字符串遍历
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello";
// 方式1:下标遍历
for (int i = 0; i < s.length(); i++) {
cout << s[i] << " ";
}
cout << endl; // H e l l o
// 方式2:范围for(C++11)
for (char c : s) {
cout << c << " ";
}
cout << endl; // H e l l o
return 0;
}
