string详解
来源:互联网 发布:长沙编程招聘信息 编辑:程序博客网 时间:2024/05/22 03:30
#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
- String 详解
- String详解
- String详解
- String 详解
- String详解
- string详解
- String详解
- string 详解
- String详解
- String详解
- String详解
- String详解
- String详解
- string 详解
- String 详解
- String 详解
- string详解
- string详解
- HDU
- hdu 1213 How Many Tables 并查集
- 安装react并创建一个demo
- 进行docker 安装并搭建tensorflow 框架用于以后tensorflow 测试学习
- Struts2学习笔记一
- string详解
- HDU-1203-I NEED A OFFER!(01背包)
- EC2 使用秘钥登录
- java常用数据结构的比较
- linux版本,Centos、Redhat、Ubuntu区别(预习)
- struts2原理小结
- 获取网路xml
- 李宏毅机器学习课程3~~~梯度下降法
- POJ1265 Area (Pick定理,多边形面积,计算几何)