C语言只是提供了Char来处理字符,对于字符串来说,只能通过字符串数组来处理,十分不方便.C++ STL提供了一个string容器来处理字符串,可以进行添加和删除和替换等等。用我们家乡话来说就是“真不孬!"
一、操作函数
s.length();//返回字符串的长度
s.append();//在尾部添加相应的字符串
s.insert(index,字符);//向字符串s的某个位置插入特定字符
s.empty();//判断字符串是否为空
s.replace(位置,距离,“替换的字符串");//从特定位置开始将特定距离的字符串替换为字符串
s.find('字符');//在s中寻找字符的位置
s.compare("字符串");//相等返回0,如果大于,返回1,小于返回-1
reverse(s.begin(),s.end());//反向排序,倒叙
s.assign(str2,4,3);//将str2的索引为4距离为3的子串赋值给s
s.erase(索引开始,距离);//删除相对应的子串
s.swap(str);//将两个字符串交换
二、代码解释
1、对于erase和length使用
#include<string>
#include<iostream>
using namespace std;
int main()
{
string s("123456");
cout<<"字符串的长度"<<s.length()<<endl;
s.erase(3);
cout<<"当前的字符串内容为:"<<s<<endl;
cout<<"当前字符串的长度为:"<<s.length()<<endl;
s="123456";
s.erase(3,1);//位置+距离
cout<<"使用erase(3,1)的操作结果"<<endl;
cout<<"当前的字符串内容为:"<<s<<endl;
cout<<"当前字符串的长度为:"<<s.length()<<endl;
return 0;
}
运行结果为:
2、对于replace函数的使用
#include<string>
#include<iostream>
using namespace std;
int main()
{
string s("123456");
cout<<"当前的字符串内容为:"<<s<<endl;
s.replace(3,3,"nihao");
cout<<"使用replace(3,3,"<<"nihao)"<<"的操作结果"<<endl;
cout<<"当前的字符串内容为:"<<s<<endl;
cout<<"当前字符串的长度为:"<<s.length()<<endl;
return 0;
}
运行结果为:
3、对于find函数的使用
#include<string>
#include<iostream>
using namespace std;
int main()
{
string s("123456");
cout<<"当前的字符串内容为:"<<s<<endl;
cout<<"2的位置"<< s.find('2')<<endl;
cout<<"4的位置"<<s.find('4')<<endl;
cout<<s.compare("1234567")<<endl;
return 0;
}
运行结果:
4、对于字符串的添加函数append和insert
#include<string>
#include<iostream>
using namespace std;
int main()
{
string s("123456");
cout<<"当前的字符串内容为:"<<s<<endl;
s.append("78");
s.insert(0,"0");
cout<<"修改之后的字符串内容为:"<<s<<endl;
return 0;
}
运行结果:
5、对于翻转函数reverse的使用
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string s("123456");
cout<<"当前的字符串内容为:"<<s<<endl;
reverse(s.begin(),s.end());
cout<<"修改之后的字符串内容为:"<<s<<endl;
return 0;
}
运行结果:
三、其他方面
对于使用printf函数输出字符串应该使用s.c_str()
#include<string>
#include<math.h>
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
string s;
char ss[100];
scanf("%s",&ss);
s=ss;
printf("%s",s.c_str());
cout<<endl;
printf("%s",ss);
cout<<endl;
cout<<s<<endl;
return 0;
}
同时string 又可以作为vector的向量元素
#include<string>
#include<math.h>
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
int main()
{
vector<string> v;
v.push_back("zhangyang");
v.push_back("hoho");
v.push_back("1232");
cout<<v[0]<<endl;
cout<<v[1]<<endl;
cout<<v[2]<<endl;
}
sscanf可以将字符串格式化分割
#include<string>
#include<math.h>
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
int main()
{
string s1,s2,s3;
char sa[100],sb[100],sc[100];
sscanf("abc 123 pc","%s %s %s",sa,sb,sc);
s1=sa;
s2=sb;
s3=sc;
cout<<s1<<" "<<s2<<" "<<s3<<" "<<endl;
int a,b,c;
sscanf("1 2 3","%d %d %d",&a,&b,&c);
cout<<a<<" "<<b<<" "<<c<<endl;
int x,y,z;
sscanf("4,5$6","%d,%d$%d",&x,&y,&z);
cout<<x<<" "<<y<<" "<<z<<endl;
return 0;
}
字符串与数字的转换
#include<iostream>
#include<cstdio>
#include<string>
#include<sstream>
using namespace std;
string convertTostring(double x)
{
ostringstream o;
if(o<<x)
return o.str();
return "conversion error";
}
double convertFromString(const string &s)
{
istringstream i(s);
double x;
if(i>>x)
return x;
return 0.0;
}
int main()
{
char b[10];
string a;
sprintf(b,"%d",1975);
a=b;
cout<<a<<endl;
string cc=convertTostring(1976);
cout<<cc<<endl;
string dd="2006";
int p=convertFromString(dd)+2;
cout<<p<<endl;
return 0;
}