1 //string字符串查找和替换  比较  存取 修改单个字符 插入和删除  string字串
2 #include <iostream>
3 #include<string>
4
5 using namespace std;
6 //查找
7
8 void test01()
9 {
10 string str1 = "abcdefg";
11
12 int pos =str1.find("de");
13
14 if (pos == -1)
15 {
16 cout << "没有找到<<endl;" <<endl;
17 }
18 else
19 {
20 cout << "找到字符串的位置,pos=" << pos << endl;
21 }
22
23 cout << "pos = " << pos << endl;
24
25 //rfind 和 find 区别
26 //rfind从右往左查找 find从左往右查找
27 pos = str1.rfind("de");
28 cout << "pos" << pos << endl;
29
30
31
32 }
33
34 //替换
35 void test02()
36 {
37 string str1 = "abcdefg";
38
39 //从1号位置起 3个字符 替换为“1111”
40 str1.replace(1, 3, "2222");
41 cout << "str1 = " << str1 << endl;
42 }
43
44 //比较
45 // = 0 >1 <-1
46 void test03()
47 {
48 string str3 = "xhello";
49 string str4 = "hellllo";
50
51 if (str3.compare(str4) == 0)
52 {
53 cout << "str1等于 str2" << endl;
54 }
55 else if (str3.compare(str4) > 0)
56 {
57 cout << "str3 大于 str4 " << endl;
58 }
59 else
60 {
61 cout << "str3 小于 str4 " << endl;
62 }
63
64 }
65
66 //string 字符存取
67 void test04()
68 {
69 string str = "hello";
70 //cout << "str = " << str << endl;
71
72 //第一种通过[]访问单个字符
73 for (int i = 0; i < str.size(); i++)
74 {
75 cout << str[i] << " ";
76 }
77 cout << endl;
78
79 //第二种通过at访问单个字符
80 for (int i = 0; i < str.size(); i++)
81 {
82 cout << str.at(i) << " ";
83 }
84 cout << endl;
85
86 //修改单个字符
87 str[0] = 'x';
88 cout << "str = " << str << endl;
89
90 str.at(1) = 'z';
91 cout << "str = " << str << endl;
92
93 }
94
95 //string 插入和删除
96
97 void test05()
98 {
99 string str8 = "hello";
100
101 //插入
102 str8.insert(1, "111");
103 cout << "str8 = " << str8 << endl;
104
105 //删除
106 str8.erase(1, 3);
107 cout << "str8 = " << str8 << endl;
108
109 }
110
111 //string 求字串
112 void test6()
113 {
114 string str = "ancdef";
115
116 string subStr = str.substr(1, 3);
117
118 cout << "subStr = " << subStr << endl;
119 }
120
121 //使用操作
122 void test7()
123 {
124 string email = "zhangsan@qq.com";
125 //从邮件地址中 获取 用户信息
126
127 int pos = email.find("@"); //8
128 cout << "emial pos = " << pos << endl;
129
130 string userName = email.substr(0, pos);
131 cout << "userName = " << userName << endl;
132 }
133
134 int main()
135 {
136 //test01();
137 //test02();
138 //test03();
139
140 //test04();
141
142 //test05();
143 //test6();
144 test7();
145 }

C++ String //string字符串查找和替换 比较 存取 修改单个字符 插入和删除 string字串的更多相关文章

  1. Java实验项目三——递归实现字符串查找和替换操作

    Program:按照下面要求实现字符串的操作: (1)设计一个提供下面字符串操作的类 1)编写一个方法,查找在一个字符串中指定字符串出现的次数. 2)编写一个方法,参数(母字符串,目标字符串,替换字符 ...

  2. 萌新笔记——C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)

    最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...

  3. C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)

    最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...

  4. linux makefile字符串操作函数 替换subst、模式替换patsubst、去首尾空格strip、查找字符串findstring、过滤filter、反过滤filter-out、排序函数sort、取单词word、取单词串wordlist、个数统计words

    1.1       字符操作函数使用 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函 ...

  5. string.Format字符串格式说明

    先举几个简单的应用案例: 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 ...

  6. string.Format字符串格式说明(转)

    转自:http://blog.csdn.net/dl020840504/article/details/8921875   先举几个简单的应用案例: 1.格式化货币(跟系统的环境有关,中文系统默认格式 ...

  7. string.Format字符串格式化说明(转)

    string.Format字符串格式化说明 www.111cn.net 编辑:Crese 来源:转载   先举几个简单的应用案例: 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统 ...

  8. [C/C++] String Reverse 字符串 反转

    #include <iostream> #include <string> #include <algorithm> #include <cstring> ...

  9. 345. Reverse Vowels of a String翻转字符串中的元音字母

    [抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

  10. Word查找和替换通配符(完全版)

    Word查找栏代码·通配符一览表 序号 清除使用通配符复选框 勾选使用通配符复选框 特殊字符 代码 特殊字符 代码or通配符 1 任意单个字符 ^? 任意单个字符 ? 2 任意数字 ^# 任意数字(单 ...

随机推荐

  1. Git - 关联远程仓库以及同时使用Lab和Hub

    更新一下,感觉有更简单的方式 就比如你git config 的 全局的name和email是lab的 那就clone github上的项目然后设置局部的name和email就行了 ********** ...

  2. 关于Curl命令的使用

    最常用的curl命令 1.发送GET请求 curl URL 例: curl URL?a=1&b=nihao 2.发送POST请求 curl -X POST -d 'a=1&b=niha ...

  3. TienChin 活动管理-搜索活动

    ActivityController @PreAuthorize("hasPermission('tienchin:activity:list')") @GetMapping(&q ...

  4. LyScript 从文本中读写ShellCode

    LyScript 插件通过配合内存读写,可实现对特定位置的ShellCode代码的导出,或者将一段存储在文本中的ShellCode代码插入到程序堆中,此功能可用于快速将自己编写的ShellCode注入 ...

  5. Asp .Net Core 系列:Asp .Net Core 配置 System.Text.Json

    目录 简介 Asp .Net Core 如何配置 System.Text.Json 所有配置 全局配置 对比 Newtonsoft.Json 无实体类型下操作 Json 自定义转换器 处理 Dynam ...

  6. GoodSync(最好的文件同步软件)

    GoodSync是一款使用创新的最好的文件同步软件,可以在你的台式机.笔记本.USB外置驱动器等设备直接进行数据文件同步软件. GoodSync将高度稳定的可靠性和极其简单的易用性完美结合起来,可以实 ...

  7. 【Python】一篇拿下类属性与类方法详解【超详细的注释和解释】

    文章目录 前言 类的实例化 类的非静态属性或方法(实例属性和方法) 类的静态属性和方法 静态属性(静态成员变量) 静态方法 类方法(静态成员函数) 总结 属性的访问权限 尾声 前言 先赞后看好习惯 打 ...

  8. PHP header的几种用法

    PHP header的几种用法 定义:header() 函数向客户端发送原始的 HTTP 报头. 1. 跳转页面 header('Location:'.$url); //Location和" ...

  9. 探索Web API SpeechSynthesis:给你的网页增添声音

    Web API SpeechSynthesis是一项强大的浏览器功能,它允许开发者将文本转换为语音,并通过浏览器播放出来.本文将深入探讨SpeechSynthesis的控制接口,包括其功能.用法和一个 ...

  10. Excel中文本型数字转换为数值型数字的方法

    背景 工作中经常遇到需要将Excel中的内容进行求和或者其他计算,但是由于格式为文本,无法进行计算和求和. 单元格的左上角都有绿色小三角,且用自动求和公式计算无法计算结果,显示为0,说明单元格格式为文 ...