avatar
fireworks99
keep hungry keep foolish

c++ string 及其函数

s.substr()

substr有2种用法:

假设:string s = “0123456789”;

string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = “56789”

string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = “567”

转自: https://blog.csdn.net/liuchuo/article/details/54599840

Code

自我感觉重要的几点:
1.getline(cin, s)的赋值
2.字符串数组的使用
5.大小、判空
6.末尾添加
11.string查找


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

int len = 4;
char ch = 'Y';

int main()
{
    ///1.赋值
    ///cin遇空格、tab、回车结束
//    string s1;
//    cin >> s1;
//    cout << s1 << '\n';
    ///吃“回车”,相当于getchar(),避免影响后续输入
//    cin.get();
    ///一行字符串的输入
//    string s2;
//    getline(cin, s2);
//    cout << s2 << '\n';



///2.字符串数组(竟然可以访问每个string里的每个字符!!!)
    string a[10];
    for(int i = 0; i < 5; ++i)
    {
        getline(cin, a[i]);
        if(i != 4)
            cin.get();
    }
    for(int i = 0; i < 5; ++i)
    {
        cout << a[i] << '\n';
    }
    ///访问一个string里的字符
    string s[5];
    s[0] = "ABCDE";
    for(int i = 0; i < 5; ++i)
    {
        cout << s[0][i] << '\n';
    }


    ///3.初始化
    cout << "1.初始化练习" << '\n';
    cout << "(1).直接等于号赋值" << '\n';
    string s3 = "嘤嘤嘤";
    cout << s3 << '\n';
    ///复制粘贴
    string s8;
    cout << "(2).字符串copy赋值" << '\n';
    s8 = s3;
    cout << s8 << '\n';
    ///用指向已知字符串的字符指针 初始化 字符串
    cout << "(3).指向已知字符串的字符指针 初始化" << '\n';
    char * s = "好傻好没用的做法";
    string s4(s);
    cout << s4 << '\n';
    cout << "(4).assign分配赋值——字符指针版" << '\n';
    s8.assign(s);
    cout << s8 << '\n';
    ///一个汉字占两个字节从1开始
    cout << "(5).assign分配前10个字符(字节)赋值——字符指针版" << '\n';
    s8.assign(s, 10);
    cout << s8 << '\n';
    ///另类复制粘贴
    cout << "(6).assign分配赋值——字符串版——相当于等于号copy" << '\n';
    s8.assign(s4);
    cout << s8 << '\n';
    ///用n个字符 初始化 一个字符串
    cout << "(8).用len个ch字符初始化" << '\n';
    string s5(6, ch);
    cout << s5 << '\n';
    ///用len个ch字符赋值
    cout << "(9).运用assign,用len个ch字符赋值" << '\n';
    string s9;
    s9.assign(len, ch);
    cout << s9 << '\n';
    ///另类赋值
    string s10 = "ABCDEFG";
    string s11;
    cout << "(10).用assign三维截取赋值" << '\n';
    s11.assign(s10, 0, 4);
    cout << s11 << '\n';
    ///迭代器赋值
    cout << "(11).用迭代器赋值" << '\n';
    s11.assign(s10.begin(), s10.end());
    cout << s11 << '\n';



    ///4.字符操作
    ///访问指定位置的字符(感觉很耗时!!!)
//    string s6 = "qaq";
//    char b = s6[1];///输出a(字符串从0开始)
//    cout << b << '\n';




  5.特性描述
    string s7 = "abcdefg";
  当前容量 15 ???
    cout << s7.capacity() << '\n';
  string 对象可存放的最大字符串长度(2147483647 = 2 ^ 31 - 1)
    cout << s7.max_size() << '\n';
  字符串大小
    cout << s7.length() << '\n';
  判空
    cout << s7.empty() << '\n';
  重定大小,不足去补
    s7.resize(10, 'A');
    cout << s7 << '\n';
    s7.resize(5, 'B');
    cout << s7 << '\n';



  6.string连接
  算数相加
    string s12 = "HIJKLMN ";
    string s13 = "OPQ ";
    s12 += s13;
    cout << s12 << '\n';
  指向字符串的字符指针截取部分 添于末尾
    s13.append(s, 10);
    cout << s13 << '\n';
    s13.append(s);
  迭代器添加
    string s14 = "s14 ";
    cout << s14 << '\n';
    s14.append(s7.begin(), s7.end());
    cout << s14 << '\n';
  尾部添字符
    string s15 = "s15 ";
    s15.push_back('A');
    cout << s15 << '\n';



  7.比较大小
    string s16 = "abcdefg";
    string s17 = "bcdefg";
    cout << (s16 == s17) << '\n';
    cout << (s16 != s17) << '\n';
    cout << (s16 > s17) << '\n';
    cout << (s16 >= s17) << '\n';
    cout << (s16 < s17) << '\n';
    cout << (s16 <= s17) << '\n';
  大于返回1,小于返回-1,等于返回0
    cout << s16.compare(s17) << '\n';
    cout << s17.compare(s16) << '\n';
  拿出从s16(从0开始)的第1个字符开始连续6个字符与s17比较
    cout << s16.compare(1, 6, s17) << '\n';
  类似的
    cout << s16.compare(3, 5, s17, 2, 5) << '\n';



  8.string子串
    string s18 = "OPQRST";
    string s19 = s18.substr(3, 3);///从下表3开始的连续3个字符
    cout << s19 << '\n';



  9.string交换
    cout << "s18: " << s18 << '\n' << "s19: " << s19 << '\n';
    s18.swap(s19);
    cout << "s18: " << s18 << '\n' << "s19: " << s19 << '\n';



  10.字符串的流处理,头文件<sstream>
    string str24("hello,this is a test");
    istringstream is(str24);
    string s20, s21, s22, s23;
    is >> s20 >> s21 >> s22 >> s23;
  s20="hello,this",s21="is",s22="a",s23="test"
    ostringstream os;
    os << s20 << s21 << s22 <<s23;
    cout<< os.str() << '\n';



  11.string的查找,查找成功时返回所在位置,
  失败时返回string::npos的值,即是-1

  括号内容为三维时,第三维是选取字符串的前几个字符

    string s25 = "ABCDEFGGGGGGHHHHHHIJKLMNOPQRSTUVWXYZ";
  从0开始找‘G’
    int pos = s25.find('G', 0);
    cout << pos << '\n';
  从0开始找“GG”
    cout << s25.find("GG", 0) << '\n';
  从0开始找“HHHHH”前4个字符所在位置
    cout << s25.find("HHHHH", 0, 4) << '\n';
  反向查找(不要以为反向查找时,最后一个字符位置是0,往前一个是1,再2...)
    cout << "反向查找,岂不是find_last_of?" << '\n';
    cout << s25.rfind('G', string::npos) << '\n';
    cout << s25.rfind("GG", string::npos) << '\n';
    cout << s25.rfind("HHHHH", string::npos, 4) << '\n';

  查找第一次出现的位置(字符串下表从0开始)
    string s26 = "ABBCCCDDDDEEEEEFFFFFFGGGGGGG";

    cout << "find_first_of检测" << '\n';///相对于find它本身不是全匹配
    cout << s26.find_first_of('C', 0) << '\n';
    cout << s26.find_first_of("DD", 0) << '\n';
    cout << s26.find_first_of("DDGG", 0, 2) << '\n';
    cout << "find_first_not_of检测" << '\n';
    cout << s26.find_first_not_of('A', 0) << '\n';
    cout << s26.find_first_not_of('C', 0) << '\n';
    cout << s26.find_first_not_of("DD", 0) << '\n';
    cout << s26.find_first_not_of("DDGG", 0, 2) << '\n';

    cout << "find_last_of检测" << '\n';
    cout << s26.find_last_of('C', string::npos) << '\n';
    cout << s26.find_last_of("DD", string::npos) << '\n';
    cout << s26.find_last_of("DDGG", string::npos, 2) << '\n';
    cout << "find_last_not_of检测" << '\n';
    cout << s26.find_last_not_of('C', string::npos) << '\n';
    cout << s26.find_last_not_of("DD", string::npos) << '\n';
    cout << s26.find_last_not_of("DDGG", string::npos, 2) << '\n';
    cout << "可以进行类似于upper_bound(0, num) - lower_bound(0, num)的操作" << '\n';



  12.string的替换
  第一维:替换起始位置
  第二维:从起始位置起连续几个字符被换掉
  第三维:要换的新字符串
  (有且仅有四维)第四维:截取新字符串前几个字符
  (有且仅有五维)第四、五维:从位置几开始截取几个字符
    string s27 = "CCCCTTTTMMMMDDDD传统美德";
    string s28;
    s28 = s27.replace(0, 4, "AAAA");
    cout << s28 << '\n';
    string s29;
    s29 = s27.replace(0, 4, "BBBB", 2);
    cout << s29 << '\n';
    string s30;
    s30 = s27.replace(0, 4, "DDDDEEEEFFFF", 4, 4);
    cout << s30 << '\n';
  前两维同上,第三维是后面字符个数
    string s31;
    s31 = s27.replace(0, 4, 8, 'Z');
    cout << s31 << '\n';
  据说以上操作中,位置可以换为迭代器



  13.string的插入(位置可以换为迭代器)
  维度意义同上
    string s32 = "tql wsl 天气冷 我睡了 ";
    string s33 = s32.insert(0, "yyy 牙痒痒 ");
    cout << s33 << '\n';
    string s34 = s32.insert(0, 4, 'L');
    cout << s34 << '\n';
    string s35 = s32.insert(0, "ZBL", 2);
    cout << s35 << '\n';
    string s36 = s32.insert(0, "ZZZBBBLLL", 3, 3);
    cout << s36 << '\n';



  14.string的删除
    string s37 = "Oh,my God!";
    string s38 = s37;
    string s39 = s37;
    string s40 = s37;
  只删除第二个
    s38.erase(s38.begin() + 2);
  从第二个开始删,保留最后一个
    s39.erase(s39.begin() + 2, s39.end() - 1);
  删除(包括)第三个字符以后的所有字符
    s40.erase(3);
    cout << s38 << '\n';
    cout << s39 << '\n';
    cout << s40 << '\n';
  有s.clear()函数

  15.补充:加法连接字符串
    string s41 = "ABC";
    string s42 = "DEF";
  不能直接连接两个字符串,如:s43 = "abc" + "def";
    string s43 = s41 + "GHI";
  s44的运算自左至右,s41 + "GHI"组成一个s(字符串变量),再加“JKL"
    string s44 = s41 + "GHI" + "JKL";
    string s45 = s41 + s42;
    s41 += "Hello world!";
    cout << s43 << '\n' << s44 << '\n' << s45 << '\n' << s41 << '\n';
    return 0;
}
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy