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. gRPC如何保障数据安全传输

    什么是 gRPC? gRPC 是由 Google 开发的高性能.开源的 RPC(Remote Procedure Call)框架,用于在客户端和服务器之间进行通信.它基于 Protocol Buffe ...

  2. C++ Qt开发:数据库与TableView多组件联动

    Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍TableVi ...

  3. MySQL 之多表连查(精简笔记)

    MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RD ...

  4. 解决SpringMVC项目Jquery引入不生效问题

    根据多方查询,总结Jquery不生效问题如下几种原因: web.xml中拦截了静态资源,但是springmvc配置文件没有对静态资源访问进行设置 <!-- <mvc:resources l ...

  5. ABC311_g One More Grid Task 题解

    题目链接:Atcoder 或者 洛谷 对于解决二维区间内的最值类型问题,我们常常有一类特别好用的方法,就是悬线法,它可以看做是单调栈的子集,但更加好理解和书写. 对于悬线法,我们有一个常见的模型,找出 ...

  6. 【路由器】电信光猫中兴 F7010C 折腾记录

    目录 问题描述 解锁超管密码 前言 配置安卓抓包环境 抓包获取超管密码 IPv6 配置 光猫拨号 改用 SLAAC 路由器配置 wan6 配置 wan 配置 lan 配置 验证 参考资料 问题描述 近 ...

  7. 关于19c RU补丁报错问题的分析处理

    本文演示关于19c RU补丁常见报错问题的分析处理: 1.查看补丁应用失败的原因 2.问题解决后可继续应用补丁 3.发现DB的RU补丁未更新 4.opatchauto应用DB补丁报错解决 1.查看补丁 ...

  8. MYSQL-另一种行转列的实现方式

    行转列的实现方式:使用mysql.help_topic --行转列 SELECT b.help_topic_id, substring_index( a.levels, ',', b.help_top ...

  9. SATA 中ATA与AHCI的区别

    SATA中ATA和AHCI有什么区别?   1.ACHI是针对SATA2设计的,可以卡其NCQ功能,表面上没有速度的优势,但是因为算法不同,可以有效的保护硬盘.ATA 是硬件模拟IDE的一种方法.表面 ...

  10. idea自定义代码片段live template

    1.介绍 有时在idea编辑器经常会写同一个代码块,那么这个代码块就可以利用live template功能把它定义成可根据关键字触发的代码片段,效果如下图: 2.操作步骤 此处我们就以springbo ...