#XXX111. 字符串
字符串
当前没有测试数据。
字符串
1、基本概念
string是C++风格的字符串,而char[](char*)是C语言风格的字符串
2、常见函数
tip:下面的字符串都是通过字符串名.函数的格式实现,这是因为这些函数都属于字符串家族的成员函数,可以理解为都是来自于字符串这个家族的,所以要通过具体的字符串名字才能找到
| 常用函数 | 功能解释 | 函数返回值类型 |
|---|---|---|
| s.size() 或者 s.length() | 返回字符串的长度 | size_t类型 |
| s.insert(插入位置i, 插入字符串s2) | 在s字符串下标为i的位置上插入srting 类型 s2 | void |
| s.erase(i,len) | 删除s字符串中下标为i开始后的len个字符 | |
| s.substr(开始位置i,子串长度len) | 截取s字符串中下标为i开始后的len个字符(当i+len 超过原字符串长度时,只取剩下的。) |
string |
| s.replace(开始位置i,长度len,要换上的字符串s2) | 从原本s字符串的位置开始,替换长度为len的子串s2 | void |
| s.find(s2,i) | 在s字符串中以i为下标位置起,查找s2整个字符串第一次出现的位置, 如果找不到返回-1 (或者string::npos) |
int(下标) |
| s.rfind(s2,i) | 和上面一样,不过是反向查找 | int |
| s.empty() | 判断字符串是否为空 , 如果空返回1 , 否则返回0 | bool |
| s.clear() | 清空字符串 | |
| getline(cin,s) | 读入一整行存储到s字符串中(直到换行),包括读入空格 | void |
| reverse(s.begin(),s.end()) | 翻转字符串 |
Tip:如果使用reverse函数,需要借助#include算法库头文件
3、函数具体使用
1、s.size() 或 s.length() —— 获取字符串长度
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "abcdef"; // 定义字符串
cout << "字符串长度为:" << s.size() << endl; // 或者用 s.length()
return 0;
}
/*
输出结果:
字符串长度为:6
*/
2、s.insert(插入位置i, 插入字符串s2) —— 在指定位置插入字符串
#include <iostream>
#include <string>
using namespace std;
int main() {
string a = "hello";
string b = "666";
a.insert(3, b); // 在a的第3个字符位置前插入b
cout << a << endl;
return 0;
}
/*
输出结果:
hel666lo
*/
3、 s.erase(i, len) —— 删除从下标 i 开始的 len 个字符
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "abcdef";
s.erase(2, 3); // 删除从索引2开始的3个字符:即删除"cde"
cout << s << endl;
return 0;
}
/*
输出结果:
abf
*/
4、 s.substr(i, len) —— 截取子串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "abcdef";
string sub = s.substr(2, 3); // 从索引2开始,取3个字符
cout << sub << endl;
return 0;
}
/*
输出结果:
cde
*/
5、s.replace(i, len, s2) —— 替换子串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "abcdef";
s.replace(2, 3, "XYZ"); // 替换从索引2开始的3个字符为"XYZ"
cout << s << endl;
return 0;
}
/*
输出结果:
abXYZf
*/
6、 s.find(s2, i) —— 从位置 i 开始查找 s2
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "ababcabc";
size_t pos = s.find("abc", 0); // 从位置0开始查找"abc"
cout << pos << endl;
return 0;
}
/*
输出结果:
2
*/
7、 s.rfind(s2, i) —— 从位置 i 向前查找 s2(反向查找)
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "ababcabc";
size_t pos = s.rfind("abc", s.size()); // 从末尾向前查找"abc"
cout << pos << endl;
return 0;
}
/*
输出结果:
5
*/
8、 s.empty() —— 判断字符串是否为空
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "";
cout << s.empty() << endl; // 空字符串返回 true (1)
return 0;
}
/*
输出结果:
1
*/
9、 s.clear() —— 清空字符串
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello";
s.clear(); // 清空字符串内容
s = "";
s = "a" + s;
cout << s.empty() << endl; // 判断是否为空
return 0;
}
/*
输出结果:
1
*/
10、 getline(cin, s) —— 读入一整行(包含空格)
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s); // 输入整行,包括空格
cout << s << endl;
return 0;
}
/*
输入样例:
hello world
输出结果:
hello world
*/
11、 reverse(s.begin(), s.end()) —— 翻转字符串
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s = "abcdef";
reverse(s.begin(), s.end()); // 翻转整个字符串
cout << s << endl;
return 0;
}
/*
输出结果:
fedcba
*/
4、关于字符串的比较逻辑
假设我们创建一个字符串a string a = "abcd";
再创建一个字符串b string b = "abce";
如果想比较a和b谁大,就只需要用if就可以直接比较
if(a > b)
cout << "a大";
else if(a < b)
cout << "b大";
else
cout << "一样大";
之所以可以这样比较,是因为,字符串是通过根据元素中每一个元素的字典序(就是按照a<b,b<c的逻辑)去进行比较的,他并不会看谁长,他只会把每一个字符,依次比较,只要有一个字符比另一个字符串的字符大,就会直接得出结论。
a = "1234"
b = "4"
判断这两个字符串表示的数字谁大
1、判断位数,位数一样,再看ASCII
2、位数不一样,位数长的一定大