文章目录
初始化方式
头文件:#include<string>
string s1 = "abc"; // 赋值构造
string s2 = string("abc"); // 调用类构造函数
string s3 = s2; // 拷贝构造
string s4(5, 's'); // s4初始化为5个's'
求长度
成员函数.length()或.size(),两个函数的源码是一样的,length是C语言延续下来的固有的习惯,size是为了和STL容器的size函数统一,参考:https://blog.csdn.net/JH_duangduang/article/details/113973585
用法:s.length()、s.size()
参数:无
返回:string类的长度
string s = "abc";
cout<<s.length()<<' ';
cout<<s.size()<<endl;
// 输出:3 3
CRUD
插入
多个字符串拼接
C++可以直接用’+’号拼接两个字符串
string s1 = "abc", s2 = "def";
string s3 = s1 + s2;
cout<<s3<<endl;
// 输出:abcdef
在固定位置插入字符串
用成员函数.insert(pos, str)
pos表示要插入的位置(下标),str表示要插入的字符串,可以是string类,也可以是char数组
string s = "abc";
s.insert(1, "123");
cout<<s<<endl;
char arr[] = "xyz";
s.insert(2, arr);
cout<<s<<endl;
/*
输出:
a123bc
a1xyz23bc
*/
查找
访问指定位置字符
直接通过下标访问
string s = "abc";
for(int i = 0; i < s.size(); i++)
cout<<s[i]<<' ';
// 输出:a b c
提取指定区间子串
成员函数.substr(pos, len)
pos表示提取开始的位置,len表示要提取的长度,如果不指明len,则提取到string末尾
返回提取到的子串
string s = "abcdef";
cout<<s.substr(2,2)<<endl;
cout<<s.substr(2)<<endl;
/*
输出:
cd
cdef
*/
查找指定内容字符串的位置
成员函数.find (str, pos = 0)
str为要查找的子串,pos为查找的起始位置,返回
str可以是string类,也可以是char数组,如果不指定pos,则从第0个字符开始
如果未查找到子串,返回string::npos,即-1
还有另一个成员函数.rfind(str,pos),和.find()用法类似,只是方向相反,从pos查找到开头
string s = "abc123def";
int a = s.find("123");
int b = s.find("123",4);
int c = s.find("xyz");
cout<<a<<' '<<b<<' '<<c<<endl;
// 输出:3 -1 -1
修改
修改指定位置的字符
- 直接用下标修改
string s = "abc";
s[1] = '2';
cout<<s<<endl;
// 输出:a2c
替换指定字符为另一个字符
举例:将str中的’b’字符全部替换成’2’
replace()函数,需要#include<algorithm>
string s = "abcabcabc";
replace(s.begin(), s.end(), 'b', '2'); //'b'全部替换成'2'
// 输出:a2ca2ca2c
替换指定位置的字符(串)
举例:将str中第2个字符开始的连续5个字符用”123”替换
成员函数.replace()
3种调用方式
string s1 = "abcdef";
string s2 = "abcdef";
string s3 = "abcdef";
s1.replace(s1.begin(), s1.end(), 3, 'O'); //将s.begin()到s.end()之间的字符串用3个’O’字符替换
s2.replace(s2.begin(), s2.end(), "123"); //将s.begin()到s.end()之间的字符串用字符串”abc”替换
s3.replace(2,3,"XYZ"); //将s中位置0开始的3个字符替换成”abc”
cout<<s1<<endl;
cout<<s2<<endl;
cout<<s3<<endl;
/*
输出:
OOO
123
abXYZf
*/
替换指定内容
举例:将str中的”Hello”全部替换成”Hey”
C++没有相关的库函数,只能自己实现
#include <string>
#include <iostream>
using namespace std;
string replace(string str, const string& from, const string& to) {
/*
参数str: 要替换的源字符串
参数from: 替换前的内容
参数to: 替换后的内容
*/
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != string::npos) { // 当源字符串中还有要替换的内容
str.replace(start_pos, from.length(), to); // 从查找到的位置,替换长度为from的长度,替换后的内容为to
start_pos += to.length(); // 替换过的部分不再检查
}
return str;
}
int main() {
string str = "Hello, World! Hello, C++!";
string from = "Hello";
string to = "Hey";
string result = replace(str, from, to);
cout << result << endl; // 输出:Hey, World! Hey, C++!
return 0;
}
上方代码块引用自:https://blog.csdn.net/weixin_44120785/article/details/128462954
删除
删除指定区间
成员函数.erase (pos=0,len=npos)
pos为删除的起始下标,len为要删除的长度,如果不指定则为从pos到结尾
string s1 = "0123456";
string s2 = "0123456";
s1.erase(3,2);
s2.erase(3);
cout<<s1<<endl;
cout<<s2<<endl;
/*
输出:
01256
012
*/
其他常用函数
比较
.compare(str) 将字符串与另一个字符串按字典序比较
参数:str为要比较的另一个字符串
返回:大于返回1,小于返回-1,相等返回0
string s1 = "abcd";
string s2 = "abcf";
string s3 = "abcd";
cout<<s1.compare(s2)<<endl; // -1
cout<<s2.compare(s1)<<endl; // 1
cout<<s1.compare(s3)<<endl; // 0
判断
函数 | 作用 |
---|---|
isalpha() | 判断字符是否是字母(‘a’-‘z’ ‘A’-‘Z’) |
isdigit() | 判断字符是否是数字 |
isspace() | 判断字符是否是空格、制表符、换行等标准空白 |
isalnum() | 判断字符是否是字母或者数字 |
ispunct() | 判断字符是标点符号 |
islower() | 判断字符是否是小写字母(‘a’-‘z’) |
isupper() | 判断字符是否是大写字母(‘A’-‘Z’) |
转换大小写
函数 | 作用 |
---|---|
tolower() | 将字符转换为小写 |
toupper() | 将字符转换为大写 |