1、string的基本概念
1.1 string的基本概念
- string是c++风格的字符串,但其本质是一个类
- string是一个类,内部封装着char*,管理这个字符串,是一个char*型的容器
- string管理的char*所分配的内存,不用担心复制越界和取值越界,由类的内部进行负责
2、string的构造函数
- string();//创建一个空字符串;string str;
- string(const char*s);//使用字符串s初始化
- string(const string&str);//使用一个string对象初始化另一个string对象
- string(int n,char c);//使用n个字符c初始化
void test()
{
/*string();
string("hello");
string(string("hello"));
string(3, 'c');*/
string s1;
const char* str = "hello world";
string s2(str);
cout << s2 << endl;
string s3(s2);
cout << s3 << endl;
string s4(3, 'c');
cout << s4 << endl;
}
3、string的赋值操作
-
string& operator=(const chars);//char类型的字符串赋值给当前字符串
-
string& operator=(const string&s);//字符串s赋值给当前字符串
-
string& operator=(char c);//字符c赋值给当前字符串
-
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赋给当前字符串
void test2()
{
const char* str = "hello world";
string str1 = "hello";
char c = 'c';
string s = str;
cout << s << endl;
string s2 = s;
cout << s2 << endl;
string s3 ;
s3 = 'c';
cout << s3 << endl;
string s4;
s4.assign("helloooooo");
cout << s4 << endl;
string s5;
s5.assign("hellooooo", 5);
cout << s5 << endl;
string s6;
s6.assign(s5);
cout << s6 << endl;
string s7;
s7.assign(5,'c');
cout << s7 << endl;
}
4、string的字符串拼接
- string& operator+=(const char*str)
- string& operator+=(const char c)
- string& operator+=(const string &str)
- string& append(const char *s)//把字符串s拼接到当前字符串的末尾
- string& append(const char *s,int n)//把字符串的前n个字符拼接到当前字符串的末尾
- string& append(const string&s)//
- string& append(const string&s,int pos,int n)//字符串s从pos开始的n个字符连接到字符串末尾
void test3()
{
const char* str = "C++";
string str2 = "Java";
string s = "hello";
//s += str;
//cout << s << endl;
///*s += 'c';
//cout << s << endl;*/
//s += s;
//cout << s << endl;
/*s.append(str);
cout << s << endl;*/
/*s.append(str, 1);
cout << s << endl;*/
/*s.append(str2);
cout << s << endl;*/
s.append(str2,2,2);
cout << s << endl;
}
5、string的查找和替换
- int find(const string& str,int pos=0) const;//查找str第一次出现的位置,从pos开始查找
- int find(const char*s,int pos=0) const;//查找s第一次出现的位置,从pos开始查找
- int find(const char*s,int pos,int n) const;//从pos位置查找s的前n个字符第一次位置
- int find(const char c,int pos=0) const;//查找字符c第一次出现的位置
- int rfind(const string& str,int pos=npos) const;//查找str最后一次出现的位置,从pos开始查找
- int rfind(const char*s,int pos=npos) const;//查找s最后一次出现的位置,从pos开始查找
- int rfind(const char*s,int pos,int n) const;//从pos位置查找s的前n个字符的最后一次位置
- int rfind(const char c,int pos=0) const;//查找字符c最后一次出现的位置
- string& replace(int pos,int n,const string& str);//替换从pos开始n个字符为字符串str
- string& replace(int pos,int n,const char*s);//替换从pos开始的n个字符为字符串s
void test4()
{
string str = "asagahajaatagajajsggaaa";
const char* st = "hello";
int position;
position= str.find("ag");//默认是从0开始的
cout << position << endl;
/*position = str.find(st);
cout << position << endl;
position = str.find("ag", 1, 2);
cout << position << endl;*/
/*position = str.rfind("aaa");
cout << position << endl;*/
/*str.replace(2, 2, "hello");
cout << str << endl;*/
str.replace(2, 2, st);
cout << str << endl;
}
6、string的字符串比较
- int compare(const string &s)const;
- int compare(const char*s)const;
void test5()
{
string s1 = "xello";
string s2 = "hello";
if (s1.compare(s2)==0)
{
cout << "s1==s2" << endl;
}
else if (s1.compare(s2) > 0)
{
cout << "s1>s2" << endl;
}
else
{
cout << "s1<s2" << endl;
}
}
7、string的字符存取
- char& operator[](int n);//通过[]方式取字符
- char& at(int n);//通过at方法获取字符
void test6()
{
string str = "hello";
//str[2] = 'u';
str.at(2) = 'u';
for (int i = 0; i < str.size(); i++)
{
//cout << str[i] << " ";
cout << str.at(i) << " ";
}
cout << endl;
}
8、string的插入和删除
- string& insert(int pos,const char*s);
- string& insert(int pos,const string& str);
- string& insert(int pos,int n,char c);//在指定位置插入n个字符串
- string& erase(int pos,int n=npos)//删除从pos开始的n个字符串
void test7()
{
string str = "hello";
//str.insert(2, "c++");
str.insert(2, 10, 'c');
cout << str << endl;
str.erase(2, 10);
cout << str<<endl;
}
9、string的子串获取
- string substr(int pos=0,int n=npos)const;//返回由pos开始的n个字符组成的字符串。
void test8()
{
/*string str = "abcdefghijk";
string s=str.substr(2, 4);
cout << s << endl;*/
string email = "zhang@sina.com";
int pos = email.find("@");
string name = email.substr(0, pos);
cout << name << endl;
}