C++ String //string字符串查找和替换 比较 存取 修改单个字符 插入和删除 string字串
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字串的更多相关文章
- Java实验项目三——递归实现字符串查找和替换操作
Program:按照下面要求实现字符串的操作: (1)设计一个提供下面字符串操作的类 1)编写一个方法,查找在一个字符串中指定字符串出现的次数. 2)编写一个方法,参数(母字符串,目标字符串,替换字符 ...
- 萌新笔记——C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)
最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...
- C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)
最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...
- linux makefile字符串操作函数 替换subst、模式替换patsubst、去首尾空格strip、查找字符串findstring、过滤filter、反过滤filter-out、排序函数sort、取单词word、取单词串wordlist、个数统计words
1.1 字符操作函数使用 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函 ...
- string.Format字符串格式说明
先举几个简单的应用案例: 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 ...
- string.Format字符串格式说明(转)
转自:http://blog.csdn.net/dl020840504/article/details/8921875 先举几个简单的应用案例: 1.格式化货币(跟系统的环境有关,中文系统默认格式 ...
- string.Format字符串格式化说明(转)
string.Format字符串格式化说明 www.111cn.net 编辑:Crese 来源:转载 先举几个简单的应用案例: 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统 ...
- [C/C++] String Reverse 字符串 反转
#include <iostream> #include <string> #include <algorithm> #include <cstring> ...
- 345. Reverse Vowels of a String翻转字符串中的元音字母
[抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
- Word查找和替换通配符(完全版)
Word查找栏代码·通配符一览表 序号 清除使用通配符复选框 勾选使用通配符复选框 特殊字符 代码 特殊字符 代码or通配符 1 任意单个字符 ^? 任意单个字符 ? 2 任意数字 ^# 任意数字(单 ...
随机推荐
- ILRuntime的TestCase
基于ILRuntime 1.6.3版本,在ILRuntime中提供测试用例,建议在下载ILRuntime之后先跑一遍官方的测试用例,对比自己使用ILRuntime的性能和官方数据是否一致 测试工具 测 ...
- pymysql基本使用规则
1.执行SQL #!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql # 创建连接 conn = pymysql.connect(ho ...
- java在服务器上创建文件(以shell脚本为例)并执行
java在服务器上创建文件(以shell脚本为例)并执行 1️⃣ 首先写个方法,来在服务器上创建脚本 package com.preciouslove.xinxin_emo.controller; i ...
- SOCKS5协议解析
socks的官方文档:https://www.ietf.org/rfc/rfc1928.txt 本文改变其他作者之手,在原文基础上加入客户端的编写,完善了服务端代码,原文是Linux端的程序代码,本文 ...
- yapi 个人空间 这个分组的问题
总结:yapi个人空间分组的问题,我暂时不用理睬 他自己自由,但是 不允许他 创建非个人空间的分组.这点留意 避免不统一.所有的分组都必须我自己来创建,不允许他们私自创建.
- 使用7-zip进行分卷压缩和解分卷压缩(Windows和Linux)
现在一共有10个视频,一共313M,我对该文件夹进行分卷压缩,每个tar包100M,压缩过程如下: Windows环境首先选中所有的压缩包,然后在压缩包上单击鼠标右键,然后选择7-Zip,再选择提取到 ...
- TDD学习笔记(二)单元测试
单元测试 定义 单元测试最早来源于Kent Beck,他在开发SmallTalk中引入了这个概念,随着软件工程学的不断发展,使得单元测试已经成为软件编程中一项非常有用的实践. 在维基百科中," ...
- JOISC 2019 记录
Day1 T1 Examination 三维数点板子题,直接 cdq分治+树状数组,时间复杂度 \(O(n\log^2n)\). Day1 T2 Meetings 对于一个大小为 \(n\) 的树,我 ...
- Innodb 存储引擎表
目录 索引组织表 Innodb逻辑存储结构 表空间 段 区 页 行 Innodb 行记录格式 Compact Redundant 行溢出数据 Compressed 和 Dynamic 行记录格式 ch ...
- python利用random模块随机生成MAC地址和IP地址
import random def randomMac(): macstring = "0123456789abcdef"*12 macstringlist=random. ...