string详解

#include <iostream>
#include <string>
using namespace std;


int main()
{
    //1.string类的构造函数
    string str1 = "aaaaa";//最简单的字符串初始化
    cout << str1 << endl; // aaaaa

    char *s = "bbbbb";
    string str2(s);//用c字符串s初始化
    cout << str2 << endl; // bbbbb

    char ch = 'c';
    string str3(5, ch);//用n个字符ch初始化
    cout << str3 << endl; // ccccc

    //2.string类的字符操作
    string str4 = "abcde";
    ch = str4[3];//operator[]返回当前字符串中第n个字符的位置
    cout << ch << endl; // d

    string str5 = "abcde";
    ch = str5.at(4);//at()返回当前字符串中第n个字符的位置,并且提供范围检查,当越界时会抛出异常!
    cout << ch << endl; // e

    //3.string的特性描述
    string str6 = "abcdefgh";
    int size = 0;
    size = str6.capacity();//返回当前容量
    cout << size << endl;  // 8
    size = str6.max_size();//返回string对象中可存放的最大字符串的长度
    cout << size << endl;  // 1073741820
    size = str6.size();//返回当前字符串的大小
    cout << size << endl; // 8
    size = str6.length();//返回当前字符串的长度
    cout << size << endl; // 8
    bool flag;
    flag = str6.empty();//判断当前字符串是否为空
    cout << flag << endl; // false,0
    int len = 15;
    str6.resize(len, 'x');//把字符串当前大小置为len,并用字符ch填充不足的部分
    cout << str6 << endl; // abcdefghxxxxxxx

    //4.string的赋值
    string str7;
    str7 = str6;//把字符串str6赋给当前字符串
    cout << str7 << endl; // abcdefghxxxxxxx
    str7.assign(str6);//把字符串str6赋给当前字符串
    cout << str7 << endl; // abcdefghxxxxxxx
    str7.assign(s);//用c类型字符串s赋值
    cout << str7 << endl; // bbbbb
    str7.assign(s, 2);//用c类型字符串s开始的n个字符赋值
    cout << str7 << endl; // bb
    str7.assign(len, 'x');//用len个字符ch赋值给当前字符串
    cout << str7 << endl; // xxxxxxxxxxxxxxx
    str7.assign(str6, 0, 3);//把字符串str7中从0开始的3个字符赋给当前字符串
    cout << str7 << endl; // abc
    string str8 = "0123456789";
    str7.assign(str8.begin(), str8.end());//把迭代器之间的字符赋给字符串
    cout << str7 << endl; // 0123456789

    //5.string的连接
    string str9 = "start";
    str9 += str8;//把字符串str9连接到当前字符串的结尾
    cout << str9 << endl; // start0123456789
    str9.append(s);//把c类型字符串s连接到当前字符串的结尾
    cout << str9 << endl; // start0123456789bbbbb
    str9.append(s, 2);//把c类型字符串s的前2个字符连接到当前字符串的结尾
    cout << str9 << endl; // start0123456789bbbbbbb
    str9.append(str8.begin(), str8.end());//把迭代器之间的一段字符连接到当前字符串的结尾
    cout << str9 << endl; // start0123456789bbbbbbb0123456789
    str9.push_back('k');//把一个字符连接到当前字符串的结尾
    cout << str9 << endl; //start0123456789bbbbbbb0123456789k

    //6.string的比较
    flag = (str8 == str9);//判断两个字符串是否相等
    cout << flag << endl; // 0
    flag = (str8 != str9);//判断两个字符串是否不相等
    cout << flag << endl; // 1
    flag = (str8 > str9);//判断两个字符串是否大于关系
    cout << flag << endl; // 0
    flag = (str8 < str9);//判断两个字符串是否为小于关系
    cout << flag << endl; // 1
    flag = (str8 >= str9);//判断两个字符串是否为大于等于关系
    cout << flag << endl; // 0
    flag = (str8 <= str9);//判断两个字符串否为小于等于关系
    cout << flag << endl; // 1

    //以下的3个函数同样适用于c类型的字符串,在compare函数中>时返回1,<时返回-1,=时返回0
    flag = str9.compare(str8);//比较两个字符串的大小,通过ASCII的相减得出!
    cout << flag << endl; // 1
    flag = str9.compare(6, 12, str8);//比较str9字符串从6开始的12个字符组成的字符串与str9的大小
    cout << flag << endl; // 1
    flag = str9.compare(6, 12, str8, 3, 5);//比较str9字符串从6开始的12个字符组成的字符串与str9字符串从3开始的5个字符组成的字符串的大小
    cout << flag << endl; // 1

    //7.string的字串
    string str10;
    str10 = str9.substr(10, 15);//返回从下标10开始的15个字符组成的字符串
    cout << str10 << endl; // 56789bbbbbbb012

    //8.string的交换
    str10.swap(str9);//交换str10与str9的值
    cout << str10 << endl; // start0123456789bbbbbbb0123456789k

    //9.string的查找,查找成功时返回所在位置,失败时返回string::npos的值,即是-1
    string str11 = "abcdefghijklmnopqrstuvwxyz";
    int pos;
    pos = str11.find('i', 0);//从位置0开始查找字符i在当前字符串的位置
    cout << pos << endl; //8
    pos = str11.find("ghijk", 0);//从位置0开始查找字符串“ghijk”在当前字符串的位置
    cout << pos << endl; // 6
    pos = str11.find("opqrstuvw", 0, 4);//从位置0开始查找字符串“opqrstuvw”前4个字符组成的字符串在当前字符串中的位置
    cout << pos << endl; // 14
    pos = str11.rfind('s', string::npos);//从字符串str11反向开始查找字符s在字符串中的位置
    cout << pos << endl; // 18
    pos = str11.rfind("klmn", string::npos);//从字符串str11反向开始查找字符串“klmn”在字符串中的位置
    cout << pos << endl; // 10
    pos = str11.rfind("opqrstuvw", string::npos, 3);//从string::pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置
    cout << pos << endl; // 14

    string str12 = "aaaabbbbccccdddeeefffggghhhiiijjjkkllmmmandjfaklsdfpopdtwptioczx";
    pos = str12.find_first_of('d', 0);//从位置0开始查找字符d在当前字符串第一次出现的位置
    cout << pos << endl; // 12
    pos = str12.find_first_of("eefff", 0);//从位置0开始查找字符串“eeefff“在当前字符串中第一次出现的位置
    cout << pos << endl; // 15
    pos = str12.find_first_of("efff", 0, 3);//从位置0开始查找当前串中第一个在字符串”efff“的前3个字符组成的数组里的字符的位置
    cout << pos << endl; // 15
    pos = str12.find_first_not_of('b', 0);//从当前串中查找第一个不在串s中的字符出现的位置
    cout << pos << endl; // 0
    pos = str12.find_first_not_of("abcdefghij", 0);//从当前串中查找第一个不在串s中的字符出现的位置
    cout << pos << endl; // 33
    pos = str12.find_first_not_of("abcdefghij", 0, 3);//从当前串中查找第一个不在由字符串”abcdefghij”的前3个字符所组成的字符串中的字符出现的位置
    cout << pos << endl; // 12
    //下面的last的格式和first的一致,只是它从后面检索!
    pos = str12.find_last_of('b', string::npos);
    cout << pos << endl; // 7
    pos = str12.find_last_of("abcdef", string::npos);
    cout << pos << endl; // 61
    pos = str12.find_last_of("abcdef", string::npos, 2);
    cout << pos << endl; // 45
    pos = str12.find_last_not_of('a', string::npos);
    cout << pos << endl; // 63
    pos = str12.find_last_not_of("abcdef", string::npos);
    cout << pos << endl; // 63
    pos = str12.find_last_not_of("abcdef", string::npos, 3);
    cout << pos << endl; // 63

    //10.string的替换
    string str13 = "abcdefghijklmn";
    str13.replace(0, 3, "qqqq");//删除从0开始的3个字符,然后在0处插入字符串“qqqq”
    cout << str13 << endl; // qqqqdefghijklmn
    str13.replace(0, 3, "vvvv", 2);//删除从0开始的3个字符,然后在0处插入字符串“vvvv”的前2个字符
    cout << str13 << endl; // vvqdefghijklmn
    str13.replace(0, 3, "opqrstuvw", 2, 4);//删除从0开始的3个字符,然后在0处插入字符串“opqrstuvw”从位置2开始的4个字符
    cout << str13 << endl; // qrstdefghijklmn
    str13.replace(0, 3, 8, 'c');//删除从0开始的3个字符,然后在0处插入8个字符 c
    cout << str13 << endl; // cccccccctdefghijklmn
    //上面的位置可以换为迭代器的位置,操作是一样的,在这里就不再重复了!

    //11.string的插入,下面的位置处亦可以用迭代器的指针表示,操作是一样的
    string str14 = "abcdefg";
    str14.insert(0, "mnop");//在字符串的0位置开始处,插入字符串“mnop”
    cout << str14 << endl; // mnopabcdefg
    str14.insert(0, 2, 'm');//在字符串的0位置开始处,插入2个字符m
    cout << str14 << endl; // mmmnopabcdefg
    str14.insert(0, "uvwxy", 3);//在字符串的0位置开始处,插入字符串“uvwxy”中的前3个字符
    cout << str14 << endl; // uvwmmmnopabcdefg
    str14.insert(0, "uvwxy", 1, 2);//在字符串的0位置开始处,插入从字符串“uvwxy”的1位置开始的2个字符
    cout << str14 << endl; // vwuvwmmmnopabcdefg

    //12.string的删除
    string str15 = "gfedcba";
    string::iterator it;
    it = str15.begin();
    it++;
    str15.erase(it);//删除it指向的字符,返回删除后迭代器的位置
    cout << str15 << endl; // gedcba
    str15.erase(it, it+3);//删除it和it+3之间的所有字符,返回删除后迭代器的位置
    cout << str15 << endl; // gba
    str15.erase(2);//删除从字符串位置3以后的所有字符,返回位置3前面的字符
    cout << str15 << endl; // gb

    string str("    abcabcssfabcsfsas    ");
    string::size_type pos(0);

    pos = str.find("abc", pos);
    while (pos != string::npos)
    {
         cout << str.substr(pos) << endl;//abcabcssfabcsfsas abcssfabcsfsas abcsfsas
         pos = str.find("abc", pos + 3);//查找所有abc的字符子串(包括abc本身)
    }

    pos = 0;
    pos = str.find("abc", pos);

    while (pos != string::npos)
    {
        str.replace(pos, 3, "");//将str所有的abc替换为空
        cout << str << endl;//abcssfabcsfsas ssfabcsfsas ssfsfsas
        pos = str.find("abc", pos);
    }

    pos = 0;
    pos = str.find_first_not_of(" ", pos);
    if (pos != string::npos)
    {
        str.erase(0, pos); //去掉string前面空格
        cout << str << endl;// "ssfsfsas    "
    }
    pos = 0;
    pos = str.find_last_not_of(" ", string::npos);
    if (pos != string::npos)
    {
        str.erase(pos+1); //去掉string后面空格
        cout << str << endl;// "ssfsfsas"
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值