代码示例:

  1. #include <iostream>
  2. #include "string"
  3. using namespace std;
  4. //字符串初始化
  5. void strInit()
  6. {
  7. cout << "字符串初始化:"  <<endl;
  8. string s1 = "abcdefg";  //初始化方式1
  9. string s2("abcdefg");   //初始化方式2
  10. string s3 = s2;         //通过拷贝构造函数 初始化s3
  11. string s4(7,'s');       //初始化7个s的字符串
  12. cout << "s1 = "<< s1 << endl;
  13. cout << "s2 = "<< s2 << endl;
  14. cout << "s3 = "<< s3 << endl;
  15. cout << "s4 = "<< s4 << endl;
  16. }
  17. //字符串遍历
  18. void strErgo()
  19. {
  20. cout << "字符串遍历:"  <<endl;
  21. string s1 = "abcdefg";  //初始化字符串
  22. //通过数组方式遍历
  23. cout << "1、通过数组方式遍历:"  <<endl;
  24. for (int i = 0; i < s1.length(); i++)
  25. {
  26. cout << s1[i] << " ";
  27. }
  28. cout << endl;
  29. //通过迭代器遍历
  30. cout << "2、通过迭代器遍历:"  <<endl;
  31. for(string::iterator it = s1.begin(); it!= s1.end(); it++)
  32. {
  33. cout << *it << " ";
  34. }
  35. cout << endl;
  36. //通过at()方式遍历
  37. cout << "3、通过at()方式遍历:"  <<endl;
  38. for (int i = 0; i < s1.length(); i++)
  39. {
  40. cout << s1.at(i) << " ";        //此方式可以在越界时抛出异常
  41. }
  42. cout << endl;
  43. }
  44. //字符指针和字符串的转换
  45. void strConvert()
  46. {
  47. cout << "字符指针和字符串的转换:"  <<endl;
  48. string s1 = "abcdefg";  //初始化字符串
  49. cout << "string转换为char*:"  <<endl;
  50. //string转换为char*
  51. cout << s1.c_str() <<endl;  //s1.c_str()即为s1的char *形式
  52. cout << "char*获取string内容:"  <<endl;
  53. //char*获取string内容
  54. char buf[64] = {0};
  55. s1.copy(buf, 7);//复制7个元素
  56. cout << buf <<endl;
  57. }
  58. //字符串连接
  59. void strAdd()
  60. {
  61. cout << "字符串连接:"  <<endl;
  62. cout << "方式1:"  <<endl;
  63. string s1 = "123";
  64. string s2 = "456";
  65. s1 += s2;
  66. cout << "s1 = "<< s1 << endl;
  67. cout << "方式2:"  <<endl;
  68. string s3 = "123";
  69. string s4 = "456";
  70. s3.append(s4);
  71. cout << "s3 = "<< s3 << endl;
  72. }
  73. int main()
  74. {
  75. //初始化
  76. strInit();
  77. cout << endl;
  78. //遍历
  79. strErgo();
  80. cout << endl;
  81. //字符指针类型和字符串转换
  82. strConvert();
  83. cout << endl;
  84. //字符串连接
  85. strAdd();
  86. cout << endl;
  87. system("pause");
  88. return 0;
  89. }

程序运行结果:

  1. 字符串初始化:
  2. s1 = abcdefg
  3. s2 = abcdefg
  4. s3 = abcdefg
  5. s4 = sssssss
  6. 字符串遍历:
  7. 1、通过数组方式遍历:
  8. a b c d e f g
  9. 2、通过迭代器遍历:
  10. a b c d e f g
  11. 3、通过at()方式遍历:
  12. a b c d e f g
  13. 字符指针和字符串的转换:
  14. string转换为char*:
  15. abcdefg
  16. char*获取string内容:
  17. abcdefg
  18. 字符串连接:
  19. 方式1:
  20. s1 = 123456
  21. 方式2:
  22. s3 = 123456
  23. 请按任意键继续. . .

C++字符串string类常用操作详解(一)【初始化、遍历、连接】的更多相关文章

  1. Linux Shell数组常用操作详解

    Linux Shell数组常用操作详解 1数组定义: declare -a 数组名 数组名=(元素1 元素2 元素3 ) declare -a array array=( ) 数组用小括号括起,数组元 ...

  2. String类内存空间详解

    java.lang.String类内存问题详解 字符串理解的难点在于其在堆内存空间上的特殊性,字符串String对象在堆内存上有两种空间: 字符串池(String pool):特殊的堆内存,专门存放S ...

  3. String类的构造方法详解

    package StringDemo; //String类的构造方法详解 //方法一:String(); //方法二:String(byte[] bytes) //方法三:String (byte[] ...

  4. java中的String类常量池详解

    test1: package StringTest; public class test1 { /** * @param args */ public static void main(String[ ...

  5. 【python+selenium的web自动化】- 元素的常用操作详解(一)

    如果想从头学起selenium,可以去看看这个系列的文章哦! https://www.cnblogs.com/miki-peng/category/1942527.html ​ 本篇主要内容:1.元素 ...

  6. 【Git使用详解】Egit的常用操作详解

    常用操作 操作 说明 Fetch 从远程获取最新版本到本地,不会自动merge Merge 可以把一个分支标签或某个commit的修改合并现在的分支上 Pull 从远程获取最新版本并merge到本地相 ...

  7. jQuery 源码分析(十四) 数据操作模块 类样式操作 详解

    jQuery的属性操作模块总共有4个部分,本篇说一下第3个部分:类样式操作部分,用于修改DOM元素的class特性的,对于类样式操作来说,jQuery并没有定义静态方法,而只定义了实例方法,如下: a ...

  8. pandas常用操作详解——info()与descirbe()

    概述 df.info():主要介绍数据集各列的数据类型,是否为空值,内存占用情况: df.describe(): 主要介绍数据集各列的数据统计情况(最大值.最小值.标准偏差.分位数等等). df.in ...

  9. pandas常用操作详解——pd.concat()

    concat函数基本介绍: 功能:基于同一轴将多个数据集合并 pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=Fa ...

随机推荐

  1. 类之间关系理解:组合>聚合>关联>依赖;实现,继承

    类之间关系理解:组合>聚合>关联>依赖:实现,继承 1. 从类之间的关系来看,不外乎以下几种 组合>聚合>关联>依赖:实现,继承 且可以分为以下两类: (1)实现, ...

  2. Vuejs——(5)v-on

    版权声明:出处http://blog.csdn.net/qq20004604   目录(?)[+]   资料来于官方文档: http://cn.vuejs.org/guide/events.html ...

  3. [IOS] 详解图片局部拉伸 + 实现图片局部收缩

    (图为微信首页右上角『+』效果) 当初还在开发WP7的时候,从IOS同事那边了解到类似微信以上功能的实现. Item条数不同,总高度也不同,这就需要将背景图片进行局部拉伸到响应的高度,并且保持上方的三 ...

  4. ORACLE 管道技术应用

    但是使用管道函数的时候是可以返回一个package里面定义的type的. create or replace package test_typeis    type test_type_record ...

  5. 将Heap RID转换成RID格式

    使用DBCC PAGE命令查看索引叶子层中的索引键(最后一个参数为3才会显示索引键对应的HEAP RID)时,可以使用下面的SQL将HEAP RID转换成 FileId:PageId:SlotNo的格 ...

  6. Metronic-最优秀的基于Bootstrap的响应式网站模版

    在所有我看到过的基于Bootstrap的网站模版中,Metronic是我认为最优秀的,其外观之友好.功能之全面让人惊叹.Metronic 是一个自适应的HTML模版,提供后台管理模版和前端内容网页模版 ...

  7. Feed back TFS 2017 RC upgrade status to product team in product group 2017.03.01

    作为微软的MVP,有一个我最喜欢的好处,就是可以与产品组(产品研发部门)有零距离接触,可以最先拿到即将发版的产品,并且和产品组沟通,对产品中出现的问题实时反馈. 看到TFS产品组吸收了自己的建议和反馈 ...

  8. 从 exe.config 读取appSettings 中的配置数据

    右键解决方案,添加引用--> System.Configuration.dll 在exe.config 中添加数据 <appSettings> <add key=" ...

  9. Angular6 学习笔记——组件详解之模板语法

    angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...

  10. .Net Core与跨平台时区

    由于开发者不熟悉不同操作系统管理时区的方式,当用.Net Core开发与时区相关的应用运行在不同操作系统上会出现错误.这片文章将会探索一下在不同操作系统上用.Net Core 使用时区信息出现的问题与 ...