自己建网站花钱吗,注册100万的公司一年缴纳多少税,wordpress开发小程序,和田地网站seo目录
1.string 介绍
2. 初始化
3.输入
4.修改string对象
5.substr截取字符串
6.插入
7.删除
8.替换
9.查找
10.其它操作 1.string 介绍 string是一种字符串类#xff0c;可以不通过定义字符数组来存储字符串#xff0c;方便对字符串的一系列操作#xff0c;使用时…目录
1.string 介绍
2. 初始化
3.输入
4.修改string对象
5.substr截取字符串
6.插入
7.删除
8.替换
9.查找
10.其它操作 1.string 介绍 string是一种字符串类可以不通过定义字符数组来存储字符串方便对字符串的一系列操作使用时要加上头文件 #includestring 2. 初始化
1常量字符串构造
string str(Hello);
2拷贝构造
string str(Hello);
string s(str);
3拷贝构造的第二种方式
string str(Hello);
string s str;
4string(size_type n,char c) 创建一个包含 n 个c的 string 对象
string str(5,a);coutstr; //输出aaaaa
(5)部分拷贝构造
string str(hello);
string str2(str,2,3); //下标2开始的3个字符coutstr2; //输出llo
3.输入 string的输入方式不止一种每一种都有细微差别 1cin 键盘输入会跳过开头的空白直到遇到下一个空白为止
string str;cinstr; //输出abcd efg coutstr; //输出abcd
2getline(cin,str) 读取一整行
string str;getline(cin,str); //输出 abcd efg coutstr; //输出 abcd efg
4.修改string对象
1通过拼接两个对象
string s1(hello);
string s2(world);
string strs1s2;coutstr; //输出helloworld
2通过append()在末尾添加
string str(hello);
str.append(world);coutstr; //输出helloworld
3通过push_back()在末尾添加一个字符
string str(hello);
str.push_back(a);coutstr; //输出helloa
5.substr截取字符串 1substr(pos,n) 返回从pos下标开始的n个字符pos默认为下标0n默认为s.size()-pos
string str(hello);
string str2str.substr(2,3);coutstr2; //输出llo
2substr(pos)
string str(hello);
string str2str.substr(2);coutstr2; //输出llo
6.插入
(1)迭代器位置插入单个字符
string str(hello);
str.insert(str.begin(),a);coutstr; //输出ahello
2迭代器位置插入多个字符
string str(hello);
str.insert(str.begin(),3,a); //插入3个acoutstr; //输出aaahello
3在下标index前插入n个字符
string str(hello);
str.insert(2,3,a);coutstr; //输出heaaallo
4下标index前插入一个常量字符串或者string对象
string str(hello);
string s(abab);
str.insert(2,s); //下标2处插入scoutstr; //输出heababllo
5下标index前插入str中的从某一下标开始的n个字符
string str(hello);
string s(abab);
str.insert(2,s,0,2); //下标2处插入s下标0开始的两个字符 coutstr; //输出heabllo
7.删除
1erase删除全部
string str(hello);str.erase(); //清空 coutstr; //输出空
2erase(pos,n) 删除下标pos开始的n个字符
string str(hello);str.erase(2,2); //下标2开始的两个字符 coutstr; //输出heo
3erase迭代器
string str(hello);str.erase(str.begin()); //删除开头一个字符 coutstr; //输出ello
8.替换
(1)replaceposns从下标pos开始删除n个字符删除后在下标pos处插入s
string str(hello);
string s(aaa);str.replace(2,2,s); //从下标2开始删除2个字符删除后在下标2处插入scoutstr; //输出 heaaao
(2)replaceposnsab从下标pos开始删除n个字符删除后在下标pos处插入s中下标a开始的b个字符
string str(hello);
string s(aaa);str.replace(2,2,s,2,1); 从下标2开始删除2个字符删除后在下标2处插入s的下标2开始的1个字符 coutstr; //输出 heao
9.查找
1finds返回s字符第一次出现的下标
string str(hello);coutstr.find(ll); //输出2
2finds,pos从字符串的 pos 位置开始查找s返回s字符第一次出现的下标
string str(hello);coutstr.find(l,3); //输出3
3rfind 与find)类似不过是从后往前找
string str(hello);coutstr.rfind(l); //输出3
4string.find_first_of() 在字符串中从指定位置开始向后默认为索引 0 处查找参数中任何一个字符首次出现的位置
string str(hello world people);coutstr.find_first_of(woooll); //输出2
5find_last_of() 方法在字符串中查找参数中任何一个字符最后一次出现的位置即从后往前找第一个
string str(hello world people);coutstr.find_last_of(woooll); //输出16
6string.find_first_not_of() 在字符串中查找第一个不包含在参数中的字符
string str(hello world people);coutstr.find_first_not_of(hwoooll); //输出1
7find_last_not_of() 在字符串中查找最后一个不包含在参数中的字符
string str(hello world people);coutstr.find_last_not_of(hwoooll); //输出17
10.其它操作
1empty判空 若字符串为空则返回真否则返回假
string str(hello world people);coutstr.empty(); //输出0
2swap 函数交换两个字符串
string s1(hello);
string s2(world);
s1.swap(s2);couts1endl; //输出world
couts2endl; //输出hello