string和char*都可以表示字符串:
string是一个类,char*是一个指向字符的指针。string封装了char*,管理这个字符串,是一个char*类型的容器。
string管理char*所分配的内存,不用考虑内存释放和越界。
string的构造函数
- string(); //默认构造函数,构造一个空的字符串
- string(const string &str); //拷贝构造函数,构造一个与str一样的string。
- string(const char *s); //用字符串s初始化
- string(int n,char c); //用n个字符c初始化
string的赋值
- string &operator=(const string &s);//把字符串s赋给当前的字符串
- string &assign(const char *s); //把字符串s赋给当前的字符串
- string &assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串
- string &assign(const string &s); //把字符串s赋给当前字符串
- string &assign(int n,char c); //用n个字符c赋给当前字符串
- string &assign(const string &s,int start, int n); //把字符串s中从start开始的n个字符赋给当前字符串
string的初始换
string s1 = "zhangsan";
string s2("lisi");
string s3 = s1;//拷贝构造函数
string s4(3,"a");
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
cout << "s3:" << s3 << endl;
cout << "s4:" << s4 << endl;
string的存取字符操作
- const char &operator[] (int n) const;
- const char &at(int n) const;
- char &operator[] (int n);
- char &at(int n);
这里注意operator[]和at()都返回字符串的第n个字符,但是使用at()时如果出现字符串数组下标越界会抛出异常,而operator[]直接会编译器编译不通过报错。
string s = "123456789";
//数组
for(int i = 0; i < s.length(); i++)
{
cout << s[i] << endl;
}
//迭代器
for(string::iterator it = s.begin(); it < s.end(); it++)
{
cout<< *it << endl;
}
{
//使用at()时会抛出异常
try
{
for(int i = 0; i < s.length()+1; i++)
{
cout << s.at(i) << endl;
}
}
catch(...)
{
cout<< "数组越界" << endl;
}
//出现数组越界引起程序的中断,不会抛出异常 error
try
{
for(int i = 0; i < s.length()+1; i++)
{
cout << s[i] << endl;
}
}
catch(...)
{
cout<< "数组越界" << endl;
}
}
从string取得const char*的操作
- const char *c_str() const; //返回一个以'\0'结尾的字符串的首地址
- int copy(char *s, int n, int pos=0) const;
字符指针和string的转换
string s = "abc";// char* ==> string
printf("s:%s \n", s.c_str());// string ==>char*
char buf[128];
s.copy(buf,2,0);//把string的数据copy到字符数组中
string的长度
- int length() const; //返回当前字符串的长度。长度不包括字符串结尾的'\0'。
- bool empty() const; //当前字符串是否为空
string字符串连接
- string &operator+=(const string &s); //把字符串s连接到当前字符串结尾
- string &operator+=(const char *s);//把字符串s连接到当前字符串结尾
- string &append(const char *s); //把字符串s连接到当前字符串结尾
- string &append(const char *s,int n); //把字符串s的前n个字符连接到当前字符串结尾
- string &append(const string &s); //同operator+=()
- string &append(const string &s,int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
- string &append(int n, char c); //在当前字符串结尾添加n个字符c
string s1 = "zhang";
string s2 = "san";
s1 += s2;
cout<< "s1:" << s1 <<endl;
string s3 = "li";
string s4 = "si";
s3.append(s4);
cout<< "s3:" << s3 << endl;
string的查找和替换
查找
- int find(char c,int pos=0) const; //从pos开始查找字符c在当前字符串的位置
- int find(const char *s, int pos=0) const; //从pos开始查找字符串s在当前字符串的位置
- int find(const string &s, int pos=0) const; //从pos开始查找字符串s在当前字符串中的位置
find函数如果查找不到,就返回-1
- int rfind(char c, int pos=npos) const; //从pos开始从后向前查找字符c在当前字符串中的位置
- int rfind(const char *s, int pos=npos) const;
- int rfind(const string &s, int pos=npos) const;
//rfind是反向查找的意思,如果查找不到, 返回-1
替换
- string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s
- string &replace(int pos, int n, const string &s); //删除从pos开始的n个字符,然后在pos处插入串s
- void swap(string &s2); //交换当前字符串与s2的值
//查找
string s = "Hello world !!!";
//从位置下标0开始,找到第一次出现“l”的位置
int index = s.find("l",0);
cout<<"index :"<<index<<endl;
//求出“l”出现的次数 每一次出现的数组下标
int offindex = s.find("l",0);
while(offindex != string::npos)
{
cout<<"offindex"<<offindex<<endl;
offindex += 1;
offindex = s.find("l",offindex);
}
//替换
string s1 = "Hello world !!!";
s1.replace(0,3,"HEL");
cout<<"s1:"<<s1<<endl;
offindex = s.find("l",0);
while(offinxes != string::npos)
{
cout<<"offindex:"<<offindex<<endl;
s.replace(offindex,1,"L");
offindex += 1;
offindex = s.find("l",offindex);
}
cout<<"替换后s:"<< s<<endl;
String的区间删除和插入
- string &insert(int pos, const char *s);
- string &insert(int pos, const string &s);
//前两个函数在pos位置插入字符串s
- string &insert(int pos, int n, char c); //在pos位置 插入n个字符c
- string &erase(int pos=0, int n=npos); //删除pos开始的n个字符,返回修改后的字符串
//删除
string s = "cpp1 cpp2 cpp3";
string::iterator it = find(s.begin(),s.end(),"c");
if(it != s.end())
{
s.erase(it);
}
cout<<"删除“c”以后的结果s:"<<s<<endl;
s.erase(s.begin(),s.end());
cout<<"s全部删除:"<<s<<endl;
cout<<"s的长度:"<<s.length()<<endl;
//插入
string s1 = "bbb";
s1.insert(0,"AAA");//头插法
s1.insert(s1.lenght(),"CCC");//尾插法
cout<<"s1:"<<s1<<endl;
string算法相关
//小写转大写
string s1 = "AAAbbb";
//1函数的入口地址 2函数对象 3预定义的函数对象
transform(s1.begin(), s1.end(), s1.begin(), toupper);
cout << "s1" << s1 << endl;
//大写转小写
string s2 = "AAAbbb";
transform(s2.begin(), s2.end(), s2.begin(), tolower);
cout << "s2:" << s2 << endl;