string容器基本概念

本质:

  • string是C++风格的字符串,而string本质上是一个类

string和char*区别:

  • char*是一个指针
  • string是一个类,类内部封装了char,管理这个字符串,是一个char型的容器。

特点:
string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

string容器构造函数

string();//创建一个空的字符串 例如: string str;
string(const char* s);//使用字符串s初始化
string(const string& str);//使用一个string对象初始化另一个string对象
string(int n, char c);//使用n个字符c初始化

#include<iostream>
using namespace std;
#include<string>
int main()
{
    //默认构造,创建一个空字符串
    string s1;
    cout << s1 << endl;
    //使用字符串str初始化
    const char* str = "hello,world";
    string s2(str);
    cout << s2 << endl;
    //使用一个string对象初始化另外一个string对象
    string s3(s2);
    cout << s3 << endl;
    //n个字符c组成string
    string s4(10, 'a');
    cout << s4 << endl;
}

string赋值操作

  1. string& operator=(const char s);//char类型字符串赋值给当前的字符串
  2. string& operator=(const string &s);//把字符串s赋给当前的字符串
  3. string& operator=(char c);//字符赋值给当前的字符串
  4. string& assign(const char *s);//把字符串s赋给当前的字符串
  5. string& assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串
  6. string& assign(const string &s);//把字符串s赋给当前字符串
  7. string& assign(int n, char c);//用n个字符c赋给当前字符串

    #include<iostream>
    using namespace std;
    #include<string>
    int main()
    {
     //第一种
     string s1;
     s1 = "hello,world";
     cout << "str1=" << s1 << endl;
     //第二种
     string s2;
     s2 = s1;
     cout << "str2=" << s2 << endl;
     //第三种
     string s3;
     s3 = 'a';
     cout << "str3=" << s3 << endl;
     string s4;
     //第四种
     s4.assign("hello,C++");
     cout << "str4=" << s4 << endl;
     //第五种
     string s5;
     s5.assign("hello,C++", 5);
     cout << "str5=" << s5 << endl;
     //第六种
     string s6;
     s6.assign(s5);
     cout << "str6=" << s6 << endl;
     //第七种
     string s7;
     s7.assign(10, 'b');
     cout << "str7=" << s7 << endl;
    }

string拼接

  1. string& operator+=(const char* str);//重载+=操作符
  2. string& operator+=(const char c);//重载+=操作符
  3. string& operator+=(const string& str);//重载+=操作符
  4. string& append(const char *s);//把字符串s连接到当前字符串结尾
  5. string& append(const char *s,int n);//把字符串s的前n个字符连接到当前字符串结尾
  6. string& append(const string &s);//同operator+=(const string&str)
  7. string& append(const string &s,int pos,int n);//字符串s中从pos开始的n个字符连接到字符串结尾

    #include<iostream>
    using namespace std;
    #include<string>
    void test1()
    {
     string s1 = "hello";
     s1 += ",world";
     cout << s1 << endl;
     s1 += '!';
     cout << s1 << endl;
     string s2 = "wow!";
     s1 += s2;
     cout << s1 << endl;
     cout << "-------------" << endl;
    }
    void test2()
    {
     string s1 = "hello";
     s1.append(",world");
     cout << s1 << endl;
     s1.append("!???", 1);
     cout << s1 << endl;
     string s2 = "wow!";
     s1.append(s2);
     cout << s1 << endl;
     string s3 = "hello,C++????";
     s1.append(s3, 0, 9);
     cout << s1 << endl;
    }
    int main()
    {
     test1();
     test2();
    }

string查找和替换

  1. int find(const string& str, int pos = o) const;//查找str第一次出现位置,从pos开始查找
  2. int find(const char* s, int pos = 0) const;//查找s第一次出现位置,从pos开始查找
  3. int find(const char* s, int pos, int n) const;//从pos位置查找s的前n个字符第一次位置
  4. int find(const char c, int pos = θ) const;//查找字符c第一次出现位置
  5. int rfind(const string& str, int pos = npos) const;//查找str最后一次位置,从pos开始查找
  6. int rfind(const char* s, int pos = npos) const;//查找s最后一次出现位置,从pos开始查找
  7. int rfind(const char* s, int pos, int n) const;//从pos查找s的前n个字符最后一次位置
  8. int rfind(const char c, int pos = o) const;//查找字符c最后一次出现位置
  9. string& replace(int pos, int n, const string& str);//替换从pos开始n个字符为字符串str
  10. string& replace(int pos, int n,const char* s);//替换从pos开始的n个字符为字符串s

    #include<iostream>
    using namespace std;
    #include<string>
    void test1()
    {
     string s1 = "hello,world!";
     int pos = s1.find("world!");
     if (pos == -1)
     {
         cout << "未找到字符串" << endl;
     }
     else {
         cout << pos << endl;
     }
     //rfind是从右往左找,find是从左往右找。
     pos = s1.rfind("world!");
     if (pos == -1)
     {
         cout << "未找到字符串" << endl;
     }
     else {
         cout << pos << endl;
     }
    }
    void test2()
    {
     string s1 = "?abcde?";
     s1.replace(1, 5, "hello,world");
     cout << s1 << endl;
     //相当于把abcde替换成了hello,world而不是只截取hello,world的前五个字符去替换。
    }
    int main()
    {
     test1();
     test2();
    }

字符串大小比较

使用compare()方法进行比较

#include<iostream>
using namespace std;
#include<cmath>
void test()
{
    string s1 = "aello";
    string s2 = "hello";
    if (s1.compare(s2) == 0)
    {
        cout << "s1==s2" << endl;
    }
    else if (s1.compare(s2) > 0)
    {
        cout << "s1>s2" << endl;
    }
    else if (s1.compare(s2) < 0)
    {
        cout << "s1<s2" << endl;
    }
}
int main()
{
    test();
}

两个字符串比较大小是逐位比较的,例如s1的第一位比s2的第一位大,就不会考虑后面的了。比较的规则是根据字符的ASCII表的大小来比较,一般来说比较字符串的大小没有任何意义。主要是用来比较字符串是否相等。

string字符存取

一种方式类似数组的[],一种是使用at()方法。既可以读取单个字符,也可以对这单个字符进行修改。

#include<iostream>
using namespace std;
#include<cmath>
void test()
{
    string s = "hello,world";
    for (int i = 0; i < s.length(); i++)
    {
        cout << s[i];
    }
    cout << endl;
    for (int i = 0; i < s.size(); i++)
    {
        cout << s.at(i);
    }
    cout << endl;
    //修改单个字符
    s[0] = 'a';
    cout << s << endl;
    s.at(0) = 'b';
    cout << s << endl;
}
int main()
{
    test();
}

string的插入和删除

  1. string& insert(int pos, const char* s);//插入字符串
  2. string& insert(int pos, const string& str);//插入字符串
  3. string& insert(int pos, int n, char c);//在指定位置插入n个字符c
  4. string& erase(int pos, int n = npos);//删除从Pos开始的n个字符
#include<iostream>
using namespace std;
#include<cmath>
void test()
{
    //插入
    string s = "hello,world";
    s.insert(1, "222");
    cout << s << endl;
    //删除
    s.erase(1, 3);
    cout << s << endl;
}
int main()
{
    test();
}

string子串

string substr(int pos=0,int n=npos)const;//返回由pos开始的n个字符组成的字符串

#include<iostream>
using namespace std;
#include<cmath>
void test()
{
    string s = "hello,world";
    string sub_s = s.substr(1, 3);
    cout << sub_s << endl;
}
//实用操作,例如切分邮箱
void test1()
{
    string email = "1256908261@qq.com";
    int pos = email.find('@');
    cout << email.substr(0, pos);
}
int main()
{
    test();
    cout << "---------------------" << endl;
    test1();
}