C语言每天提供专门的字符串类型,需要通过字符数组才能对字符串进行存储和处理。在标准C++中,字符串类由C++ STL实现。string是一个基于字符的序列容器,具有vector向量一样的内部线性结构,字符逐一写入容器,最后以null字符结尾。跟传统的char*字符数组相比,string提供了丰富的函数用于字符的添加、删除、替换、查找和比较等。
创建string对象
主要有以下几种方式。
(1) string()
string s;
(2) string(conststring&s,size_type pos=0,size_type n=npos)
string s1;
string s2(s1);
(3) string(const char*)
char* cArray=”abc”;
string s(cArray);
字符的添加
字符的添加主要有push_back、append、insert等函数。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1;
s1.push_back('a');
s1.push_back('b');
s1.push_back('c');
cout<<s1<<endl;
char* cArray="efgh";
string s2(cArray);
cout<<s2<<endl;
cout<<s1+s2<<endl;
cout<<s1.append(s2)<<endl;
string::iterator i;
i=s1.begin();
i++;
s1.insert(i,'8');
cout<<s1<<endl;
s1+=s2;
cout<<s1<<endl;
return 0;
}
字符的遍历
可以使用数组方式和迭代器[支持反向迭代器]两种方式访问string中的字符。
#include<iostream>
#include<string>
using namespace std;
int main()
{
char* cArray="hello,world";
string s(cArray);
//数组方式
for(int i=0;i<s.size();i++)
{
cout<<s[i]<<endl;
}
//迭代器方式
string::reverse_iterator rbegin,rend;
rend=s.rend();
for(rbegin=s.rbegin();rbegin!=rend;rbegin++)
{
cout<<*rbegin<<endl;
}
return 0;
}
字符的删除
利用erase函数和clear函数可以实现字符的删除。
#include<iostream>
#include<string>
using namespace std;
int main()
{
char* cArray="a1234567";
string s(cArray);
s.erase(s.begin());
cout<<s<<endl;//1234567
s.erase(s.begin()+2,s.end()-2);
cout<<s<<endl;//1267
s.erase(0,2);
cout<<s<<endl;//67
s.clear();
cout<<s<<endl;//
return 0;
}
字符的替换
利用replace函数可以实现字符的替换。
#include<iostream>
#include<string>
using namespace std;
int main()
{
char* cArray="hello,boy!";
string s(cArray);
s.replace(6,3,"girl");
cout<<s<<endl;//hello,girl!
s.replace(10,1,1,'.');
cout<<s<<endl;//hello,girl.
s.replace(s.begin(),s.begin()+5,"good morning");
cout<<s<<endl;//good morning,girl.
return 0;
}
字符的搜索
利用find、rfind、find_first_of、find_first_not_of、find_last_of、find_last_not_of等函数可以实现字符的搜索,返回字符串的位置,找不到,返回-1.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s("dog bird chicken bird cat");
//字符串查找
cout<<s.find("bird")<<endl;//打印4
cout<<(int)s.find("fish")<<endl;//-1
//字符查找
cout<<s.find('i',0)<<endl;//5
//从字符串的末尾开始查找字符串
cout<<s.rfind("bird")<<endl;//17
//从字符串的末尾开始查找字符
cout<<s.rfind('i')<<endl;//18
//查找第一个属于某子串的字符
cout<<s.find_first_of("33r97")<<endl;//6
//查找第一个不属于某字符串的字符
cout<<s.find_first_not_of("dog bird")<<endl;//9
//查找最后一个属于某子串的字符
cout<<s.find_last_of("r")<<endl;//19
//查找最后一个不属于某字符串的字符
cout<<s.find_last_not_of("tea")<<endl;//22
return 0;
}
字符串的比较
利用compare函数实现比较。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1("abcdef");
string s2("abc");
cout<<s1.compare("abcdef")<<endl;//相等,返回0
cout<<s1.compare(s2)<<endl;//s1>s2,返回正值
cout<<s1.compare("aby")<<endl;//s1<"aby" ,返回-1
cout<<s1.compare(0,3,s2)<<endl;//s1的前3个字符等于s2,返回0
return 0;
}
其他常用函数
还有其他函数用以给出string容器的统计信息,如length、size、empty、c_str等。
这里只是给出了部分函数,还有相当多的函数没有列出,具体可以参考C++ Reference这个网站,http://www.cplusplus.com/reference/string/string/,该网站很详细地列出了各种用法。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout<<s.empty()<<endl;//空串,返回1
s+="1234567";
cout<<s.empty()<<endl;//0
cout<<s.length()<<endl;//7
cout<<s.size()<<endl;//7
const char* cArray=s.c_str();//将string对象转换为字符数组
cout<<cArray[2]<<endl;//3
return 0;
}