1. string类
1.1 常见函数
1.1.1 获取字符
#include <string>
#include<iostream>
using namespace std;
int main()
{
string str="God bless you!";
cout<<str[1]<<endl;//直接通过下标进行索引
cout<<str.at(1)<<endl;//通过函数返回字符
return 0;
}
1.1.2 字符串属性
#include <string>
#include<iostream>
using namespace std;
int main()
{
string str="God bless you!";
cout<<str.size()<<endl;//返回字符串长度
cout<<str.length()<<endl;//返回字符串长度
cout<<str.empty()<<endl;//判断字符串是否为空,为空返回1,否则返回0
cout<<str.back()<<endl;//输出字符串的尾部
cout<<*str.begin()<<endl;//输出字符串的头部
return 0;
}
1.1.3 交换字符串
#include <string>
#include<iostream>
using namespace std;
int main()
{
string str1="God bless you!";
string str2="Father be with you!";
str1.swap(str2);//把 str1 与 str2 交换
cout<<str1<<endl;
return 0;
}
1.1.4 删除字符串
#include <string>
#include<iostream>
using namespace std;
int main()
{
string str1="God bless you!";
string str2="Father be with you!";
str1.erase(3);//删除[3]及以后的字符,并返回新的字符串
str2.erase(3,5);//删除从 [3] 开始的后 5 个字符,并返回新字符串
cout<<str1<<endl;
cout<<str2<<endl;
return 0;
}
1.1.5 追加字符(串)
#include <string>
#include<iostream>
using namespace std;
int main()
{
string str = "God bless you!";
str.push_back('&');//在str的末尾添加字符'&',注意只能是字符,不能是字符串如"&"
str.append("Father be with you!");//在 str 末尾添加字符串
cout<<str<<endl;
return 0;
}
1.1.6 插入字符串
#include <string>
#include<iostream>
using namespace std;
int main()
{
string str = "God bless you!";
str.insert(0,"Father&");//从位置0开始添加字符串
cout<<str<<endl;
str.insert(0,"Maybe",3);//从位置0开始插入字符串"Maybe”的前三个即“May”
cout<<str<<endl;
str.insert(0,"You Wish",4,4);//从位置0开始插入子串"You Wish"从[4]开始的后四个字符,即"Wish"
cout<<str<<endl;
return 0;
}
1.1.7 替换字符串
#include <string>
#include<iostream>
using namespace std;
int main()
{
string str = "God bless you!";
str.replace(0,3,"Father");//把str从[0]开始的3个字符,即God替换为Father
cout<<str<<endl;
str.replace(0,6,"God be with you!",3);//把str从[0]开始的6个字符,即Father替换God
cout<<str<<endl;
return 0;
}
1.1.8 获取字符串子串
#include <string>
#include<iostream>
using namespace std;
int main()
{
string str = "God bless you!";
cout<<str.substr(4)<<endl;//返回从[4]开始的后所有字符,即bless you!
cout<<str.substr(4,5)<<endl;//返回[4]以后的5个字符,即bless
return 0;
}
1.1.9 查找子串
#include <string>
#include<iostream>
using namespace std;
int main()
{
string str = "God bless you!";
cout<<str.find("God")<<endl;//返回God在str中的起始位置,即0
cout<<str.find("God",3)<<endl;//在 str[3]~str[n-1] 范围内查找并返回字符串 God 在 str 的位置,即npos=4294967295
cout<<string::npos<<endl;
cout<<str.rfind("God",3)<<endl;//在 str[0]~str[3] 范围内查找并返回字符串 God 在 str 的位置,即0
return 0;
}
1.1.10 运算符重载
#include <string>
#include<iostream>
using namespace std;
int main()
{
string str1 = "God bless you!";
string str2 = "Father be with you!";
cout<<"May "+str1+str2<<endl;//字符串连接,至少有一个string类型
// cout<<"God"+"Bless"+"You!"<<endl; 这是不行的,会报错的
cout<<(str1==str2)<<endl;//输出false,即0
string str = s + '!'+ '!'; // 正确
cout << str << endl;
return 0;
}
1.2 常见应用
1.2.1 去除一个字符串中的所有空格
#include <string>
#include<iostream>
using namespace std;
/*返回去除空格的字符串*/
string rmSpace(string s){
while(s.find(" ")!=s.npos){
s.replace(s.find(" "),1,"");
}
return s;
}
int main()
{
string str;
getline(cin,str);
str = rmSpace(str);
cout<<str<<endl;
return 0;
}
1.2.2 将一个字符串统一大小写
#include <string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string str;
getline(cin,str);
transform(str.begin(),str.end(),str.begin(),::toupper);//统一转换为大写
cout<<str<<endl;
transform(str.begin(),str.end(),str.begin(),::tolower);//统一转换为小写
cout<<str<<endl;
return 0;
}
1.2.3 字符串分割
1) 使用find函数和substring函数
#include<iostream>
#include<string>
#include<vector>
vector<string> split(const string &str, const string &pattern)
{
vector<string> res;
if(str == "")
return res;
//在字符串末尾也加入分隔符,方便截取最后一段
string strs = str + pattern;
size_t pos = strs.find(pattern);
while(pos != strs.npos)
{
string temp = strs.substr(0, pos);
res.push_back(temp);
//去掉已分割的字符串,在剩下的字符串中进行分割
strs = strs.substr(pos+1, strs.size());
pos = strs.find(pattern);
}
return res;
}
2)使用stringstream
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
vector<string> split(const string &str, const char pattern)//注意pattern只能是字符,如" "是错误的
{
vector<string> res;
stringstream input(str); //读取str到字符串流中
string temp;
//使用getline函数从字符串流中读取,遇到分隔符时停止,和从cin中读取类似
//注意,getline默认是可以读取空格的
while(getline(input, temp, pattern))
{
res.push_back(temp);
}
return res;
}
1.2.4 字符串类型转换
#include<iostream>
#include<string>
using namespace std;
int main()
{
//int --> string
int i = 5;
string s = to_string(i);
cout << s << endl;
//double --> string
double d = 3.14;
cout << to_string(d) << endl;
//long --> string
long l = 123234567;
cout << to_string(l) << endl;
//char --> string
char c = 'a';
cout << to_string(c) << endl; //自动转换成int类型的参数
//char --> string
string cStr; cStr += c;
cout << cStr << endl;
s = "123.257";
//string --> int;
cout << stoi(s) << endl;
//string --> int
cout << stol(s) << endl;
//string --> float
cout << stof(s) << endl;
//string --> doubel
cout << stod(s) << endl;
return 0;
}
1.2.5 实现trim函数
string& Trim(string &s)
{
if(s.empty()){
return s;
}
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ")+1,s.size()-1);
return s;
}
2. 字符处理
2.1 string转char
#include <string>
#include<iostream>
using namespace std;
int main()
{
string str = "God bless you!";
char c = str[0];//直接获取字符
cout<<c<<endl;
char b = c + 32;//直接进行ASCII的运算,输出g
cout<<b<<endl;
return 0;
}
2.2 char转string
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = "god bless you!";
string ch_s = to_string(str[0]);
cout<<ch_s<<endl;// 103,输出的是ASSII的值哦
cout<<typeid(ch_s).name()<<endl;
string ch_s(1,str[0]); //利用string的构造函数
cout<<ch_s<<endl;//
cout<<typeid(ch_s).name()<<endl;
return 0;
}
2.3 判断字符的类型
#include <iostream>
using namespace std;
int main() {
cout << isdigit('8') << endl; // 判断是否是数字
cout << isalnum('g') << endl; // 判断是否是字母或数字
cout << isspace(' ') << endl; // 判断是否是空格
cout << islower('a') << endl; // 判断是否是小写
cout << isupper('A') << endl; // 判断是否是大写
cout << isalpha('y') << endl; // 判断是否是字母
cout << tolower('G') << endl; // 将大写字母转为小写字母
cout << toupper('d') << endl; // 将小写字母转为大写字母
return 0;
}
本文详细介绍了C++中string类的常用函数,包括字符获取、属性操作、字符串交换、删除、追加、插入、替换、子串获取和查找等功能。同时,文章还展示了如何去除字符串中的空格、统一大小写、字符串分割、类型转换等实用技巧,以及如何实现trim函数。此外,还讲解了字符处理,如string与char之间的转换和字符类型的判断。
373

被折叠的 条评论
为什么被折叠?



