#10047. 字符串函数笔记
字符串函数笔记
C++ 字符串常用函数笔记
一、什么是 C++ 函数?
函数是一段可以重复调用的代码,用来完成某项具体任务。
例如:
int add(int a, int b) {
return a + b;
}
调用函数:
int result = add(2, 3); // result 的值为 5
C++ 的 string 类型也提供了很多现成的函数,例如查找、删除、截取、替换和插入字符串。
使用 string 前需要包含头文件:
#include <iostream>
#include <string>
using namespace std;
字符串中的位置从 0 开始计算:
字符串: H e l l o
下标: 0 1 2 3 4
二、查找函数 find()
作用
在字符串中查找指定字符或子字符串第一次出现的位置。
基本格式
字符串.find(要查找的内容);
也可以指定从哪个位置开始查找:
字符串.find(要查找的内容, 开始位置);
示例
string str = "hello world";
size_t pos = str.find("world");
cout << pos << endl;
输出:
6
因为 "world" 的第一个字符 w 位于下标 6。
判断是否查找成功
如果没有找到,find() 会返回 string::npos。
string str = "hello world";
if (str.find("C++") != string::npos) {
cout << "找到了" << endl;
} else {
cout << "没有找到" << endl;
}
输出:
没有找到
从指定位置开始查找
string str = "apple apple";
size_t pos = str.find("apple", 1);
cout << pos << endl;
输出:
6
查找从下标 1 开始,因此跳过了第一个 "apple"。
三、删除函数 erase()
作用
删除字符串中指定位置的字符。
基本格式
字符串.erase(开始位置, 删除长度);
示例
string str = "hello world";
str.erase(5, 6);
cout << str << endl;
输出:
hello
说明:
原字符串:hello world
↑
开始位置:下标 5
删除长度:6,包括空格和 world
删除指定位置之后的全部内容
string str = "hello world";
str.erase(5);
cout << str << endl;
输出:
hello
erase(5) 会删除下标 5 以及后面的所有字符。
删除全部内容
string str = "hello world";
str.erase();
cout << str << endl;
此时 str 变成空字符串。
四、截取函数 substr()
作用
从原字符串中截取一部分内容,并返回一个新的字符串。
基本格式
字符串.substr(开始位置, 截取长度);
示例
string str = "hello world";
string result = str.substr(6, 5);
cout << result << endl;
输出:
world
说明:
字符串:h e l l o w o r l d
下标: 0 1 2 3 4 5 6 7 8 9 10
↑
从下标 6 开始截取 5 个字符
截取指定位置之后的全部内容
string str = "hello world";
string result = str.substr(6);
cout << result << endl;
输出:
world
注意:substr() 不会修改原字符串,而是返回一个新字符串。
五、替换函数 replace()
作用
将字符串中的一部分内容替换成新的内容。
基本格式
字符串.replace(开始位置, 替换长度, 新内容);
示例
string str = "I like Java";
str.replace(7, 4, "C++");
cout << str << endl;
输出:
I like C++
说明:
原字符串:I like Java
下标: 0 1 2 3 4 5 6 7 8 9 10
↑
从下标 7 开始,将 4 个字符 Java 替换成 C++
新内容和被替换内容的长度可以不同:
string str = "I like C";
str.replace(7, 1, "C++ programming");
cout << str << endl;
输出:
I like C++ programming
六、插入函数 insert()
作用
在字符串的指定位置插入新的内容。
基本格式
字符串.insert(插入位置, 要插入的内容);
示例
string str = "hello world";
str.insert(6, "beautiful ");
cout << str << endl;
输出:
hello beautiful world
说明:
原字符串:hello world
↑
在下标 6 的字符 w 前面插入 beautiful
在字符串开头插入
string str = "world";
str.insert(0, "hello ");
cout << str << endl;
输出:
hello world
在字符串末尾插入
string str = "hello";
str.insert(str.length(), " world");
cout << str << endl;
输出:
hello world
七、综合示例
下面的程序演示了这五个常用函数:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "I like Java programming";
// 1. 查找
size_t pos = str.find("Java");
if (pos != string::npos) {
cout << "Java 的位置:" << pos << endl;
}
// 2. 替换
str.replace(pos, 4, "C++");
cout << "替换后:" << str << endl;
// 3. 插入
str.insert(0, "Today, ");
cout << "插入后:" << str << endl;
// 4. 截取
string part = str.substr(7, 10);
cout << "截取结果:" << part << endl;
// 5. 删除
str.erase(0, 7);
cout << "删除后:" << str << endl;
return 0;
}
运行结果:
Java 的位置:7
替换后:I like C++ programming
插入后:Today, I like C++ programming
截取结果:I like C++
删除后:I like C++ programming
八、函数对比总结
| 函数 | 作用 | 常用格式 | 是否修改原字符串 |
|---|---|---|---|
find() |
查找内容的位置 | str.find("内容") |
否 |
erase() |
删除指定内容 | str.erase(位置, 长度) |
是 |
substr() |
截取一部分内容 | str.substr(位置, 长度) |
否 |
replace() |
替换指定内容 | str.replace(位置, 长度, "新内容") |
是 |
insert() |
插入新内容 | str.insert(位置, "内容") |
可以简单记忆为:
find → 找
erase → 删
substr → 截
replace → 换
insert → 插
注意事项:
- 字符串下标从
0开始。 find()没有找到内容时返回string::npos。substr()返回新字符串,不会修改原字符串。erase()、replace()和insert()会直接修改原字符串。- 使用位置和长度时,要注意不要超出字符串的有效范围。