#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int  main() {//string本质是一个类string s1;//默认构造const char* str = "hello world";string s2(str);cout << s2 << endl;string s3(s2);cout << s3 << endl;string s4(11, 'a');cout << s4 << endl<<endl;string str1;//拷贝构造str1 = "hello world";cout << str1<<endl;string str2 = str1;cout << str2 << endl;string str3 ;//①str3 = 'a';//②两部只能分开写不能写在一块cout << str3 << endl;string str4;str4.assign("hello world");cout << str4 << endl;string str5;str5.assign(str4, 6);//输出包括第六个(从0开始)字母以后的东西cout << str5 << endl;string str6;str6.assign("hello world",5);//输出前五个cout << str6 << endl;string str7;str7.assign(str6);//复制cout << str7 << endl;string str8;str8.assign(10,'a');//数字在前cout << str8 << endl << endl;
}

字符串拼接

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int  main() {//字符串拼接string s1 = "我";cout << s1 << endl;s1 += "喜欢摸鱼";//追加多个cout << s1 << endl;s1 += ':';//追加一个cout << s1 << endl;string s2 = "上厕所";s1 += s2;cout << s1 << endl;//利用appendcout << endl;string ss1 = "I";ss1.append(" love moyu:");cout << ss1 << endl;ss1.append("play game", 4);//前4个拼接到尾部cout << ss1 << endl;ss1.append(" lol ");cout << ss1 << endl;ss1.append("play game",5, 4);//从下标5开始的4个字符cout << ss1 << endl;
}

string查找和替换

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int  main() {//字符串查找string s1 = "abcabcabcde";string kid1 = "abcde";string kid2 = "ad";int pos = s1.find(kid1);pos == -1 ? cout<<"不存在字串:"<<kid1 : cout <<"存在这个字串:"<<kid1 << endl;int pos1 = s1.find(kid2);pos1 == -1 ? cout << "不存在字串:" << kid2 : cout << "存在这个字串:" << kid2 << endl;//rfind从右向左找,find从左向右找//字符串替换string t1 = "abcedf";t1.replace(1, 3, "1111");cout <<endl<< t1 << endl;
}

字符串对比

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int  main() {//字符串比较//相同时为 0 大为1 小为0string s1 = "abcde";string s2_1 = "abcde", s2_2 = "abc";if (s1.compare(s2_1) == 0)cout << "字符串相同" << endl;if (s1.compare(s2_2) == 0)cout << "字符串相同" << endl;else if (s1.compare(s2_2) >0)cout << s1 << endl;else cout << s2_2 << endl;
}

单个字符读写

【at的运用】

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int  main() {//字符串存取string s1 = "hello";cout << "s1 = "<<s1 << endl;for (int i = 0; i < s1.size(); i++)cout << s1[i] << " ";cout << endl;for (int i = 0; i < s1.size(); i++)cout << s1.at(i) << " ";cout << endl;//修改s1[0] = 'a';cout << s1 << endl;s1.at(1) = 'b';cout << s1 << endl;
}

字符串插入和删除

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int  main() {//字符串插入和删除string s1 = "aaaaa";cout << s1 << endl;//插入 inserts1.insert(1, "111");cout << s1 << endl;//删除erases1.erase(1, 3);//从1开始删除3个cout << s1 << endl;
}

字符串子串查找

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int  main() {//字符串子串string s = "hello c++";cout << s << endl;string kid1 = s.substr(0, 5);//从0开始截取5个cout << kid1 << endl;string email = "helloc++@163.com";//截取不同用户名(@163.com是共同属性)string information = email.substr(0, email.rfind('@'));cout << information << endl;
}

C++字符串赋值、拼接、查找、替换、存取、插入删除和子串相关推荐

  1. 【C++编程语言】之string容器 基本概念 构造函数 赋值 拼接 查找 替换比较 插入 删除 子串获取

    目录 1.string基本概念 2.string构造函数 3.string赋值操作 4.string字符串拼接 5.string查找和替换 6.string字符串比较 7.string单个字符的访问和 ...

  2. oracle操作字符串:拼接、替换、截取、查找

    1.拼接字符串 1)可以使用"||"来拼接字符串 1 select '拼接'||'字符串' as str from dual 2)通过concat()函数实现 1 select c ...

  3. oracle 全局搜索字符串,oracle操作字符串:拼接、替换、截取、查找 _ 学编程-免费技术教程分享平台...

    1.拼接字符串 1)可以使用"||"来拼接字符串 1 select '拼接'||'字符串' as str from dual 2)通过concat()函数实现 1 select c ...

  4. oracle 拼接 长度,oracle操作字符串:拼接、替换、截取、查找、长度、判断详解...

    1.拼接字符串 1)可以使用"||"来拼接字符串 select '拼接'||'字符串' as str from dual 2)通过concat()函数实现 select conca ...

  5. oracle截取字符串替换,oracle操作字符串:拼接、替换、截取、查找、长度、判断...

    1.拼接字符串 1)可以使用"||"来拼接字符串 select '拼接'||'字符串' as str from dual 2)通过concat()函数实现 select conca ...

  6. c语言字符串替换有大小写,(C语言)字符串大小写无关查找替换

    [问题描述] 编写程序将一行英文中指定的字符串替换为另一字符串后输出.注意:查找指定字符串时,大小写无关,而且有可能多次出现. [输入形式] 先从标准输入中分行输入两个英文字符串(每个字符串不为空,长 ...

  7. (C语言)字符串大小写无关查找替换

    [问题描述] 编写程序将一行英文中指定的字符串替换为另一字符串后输出.注意:查找指定字符串时,大小写无关,而且有可能多次出现. [输入形式] 先从标准输入中分行输入两个英文字符串(每个字符串不为空,长 ...

  8. 用c语言实现单链表的初始化,建表,查找,求长度,插入,删除等操作,【YTU+2430+C语言习题+链表建立+插入+删除+输(5)...

    的打印.判断链表是否为空.计算链表长度.插入节点.删除节点.删除整个链表.(2) 线性表adt顺序存储实现中的创建.查找.插入和删除等基本操作及相关算法,线性表adt链式存储实现中单链表.循环链表和双 ...

  9. C++知识点30——使用C++标准库(关联容器map及其初始化,赋值,查找,添加,删除与迭代器失效)

    一.关联容器简介 关于顺序容器和关联容器的区别已经在博客https://blog.csdn.net/Master_Cui/article/details/107427911中提过 C++标准库中的关联 ...

最新文章

  1. c#中什么情况下用(int)什么情况下用Convert.ToInt32
  2. Java反序列化漏洞整理
  3. Struts2拦截器属性excludeMethods、includeMethods配置无效之解决方法
  4. SuperMap 房产政务协同管理平台
  5. 基于select模型的TCP服务器
  6. 记录x86调试命令总结
  7. [JS-DOM]事件监听机制
  8. spring boot常用注解的作用
  9. 大卫谈学习5:成长的烦恼
  10. 看《墨攻》理解IoC—Spring底层核心
  11. MYSQL 取上一条记录,与下一条记录
  12. java 解析 svg_如何解析Java / Android中的SVG?
  13. 赤裸裸的逆袭:小学生都用大数据分析苏轼了!
  14. mysql中使用order by 排序时使用if()函数应用场景。
  15. Python对列表数据进行区间统计
  16. 太湖2018年渔业产值达7.3亿元 今年大闸蟹产量将降低
  17. js图片加载 完成之前显示loading中的图片
  18. 使用win32API获取打印机是否处于脱机状态
  19. php微信公众号向指定客服发信息,微信公众号给用户发送一条消息 客服消息
  20. requests_模拟搜狗翻译

热门文章

  1. Linux命令 --- cp与scp
  2. 计算机PPT教材教法,小语教材教法讲义课件_电脑基础知识_it计算机_专业资料.ppt...
  3. 一加7t人脸识别_90Hz新品,一加7T系列国内发布日期官宣
  4. 【Java】MacOS Eclipse使用JOL观察对象布局(详解)
  5. Opencv之cvtColor
  6. 国内外常用数据库介绍
  7. 阿里巴巴iconfont字体图标使用方法
  8. Deepin禁用usb声卡
  9. 基于ns2和ns3进行AODV协议改进仿真
  10. 软件测试行业的优缺点