string截取、替换、查找子串函数,find_first_of 用法
1. 截取子串
s.substr(pos, n) 截取s中从pos开始(包括0)的n个字符的子串,并返回
s.substr(pos) 截取s中从从pos开始(包括0)到末尾的所有字符的子串,并返回
2. 替换子串
s.replace(pos, n, s1) 用s1替换s中从pos开始(包括0)的n个字符的子串
3. 查找子串
s.find(s1) 查找s中第一次出现s1的位置,并返回(包括0)
s.rfind(s1) 查找s中最后次出现s1的位置,并返回(包括0)
s.find_first_of(s1) 查找在s1中任意一个字符在s中第一次出现的位置,并返回(包括0)
s.find_last_of(s1) 查找在s1中任意一个字符在s中最后一次出现的位置,并返回(包括0)
s.fin_first_not_of(s1) 查找s中第一个不属于s1中的字符的位置,并返回(包括0)
s.fin_last_not_of(s1) 查找s中最后一个不属于s1中的字符的位置,并返回(包括0)
一:find
函数原型:
size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;
参数说明:pos查找起始位置 n待查找字符串的前n个字符
使用样例:
string str1("the usage of find can you use it");
string str2("the");
上面定义出了两个字符串;
str1.find(str2); // 从串str1中查找时str2,返回str2中首个字符在str1中的地址
str1.find(str2,5); // 从str1的第5个字符开始查找str2
str1.find("usage"); // 如果usage在str1中查找到,返回u在str1中的位置
str1.find("o"); // 查找字符o并返回地址
str1.find("of big",2,2); //从str1中的第二个字符开始查找of big的前两个字符
二:find_first_of
函数原型:
size_t find_first_of ( const string& str, size_t pos = 0 ) const;
size_t find_first_of ( const char* s, size_t pos, size_t n ) const;
size_t find_first_of ( const char* s, size_t pos = 0 ) const;
size_t find_first_of ( char c, size_t pos = 0 ) const;
参数和find基本相同,不再赘述!
特别注意:
find_first_of 函数最容易出错的地方是和find函数搞混。它最大的区别就是如果在一个字符串str1中查找另一个字符串str2,如果str1中含有str2中的任何字符,则就会查找成功,而find则不同;
比如:
string str1("I am change");
string str2("about");
int k=str1.find_first_of(str2); //k返回的值是about这5个字符中任何一个首次在str1中出现的位置;
代码示例:
#include<iostream>
#include<string>
using namespace std;
int main(){
string str1("hi,every one! I am heat_nan from dlut one");
string str2("heat_nan");
int k = str1.find(str2);
cout << "the position of 'heat_nan' is " << k << endl;
int k1 = str1.find("one");
cout << "the position of the first 'one' is " << k1 << endl;
int k2 = str1.find("one of", k1 + 1, 3);
cout << "the position of the second 'one' is " << k2 << endl;
int k3 = str1.find_first_of("aeiou");
while (k3 != string::npos){
str1[3] = '*';
k3 = str1.find_first_of("aeiou", k3 + 1);
}
cout << str1 << endl;
system("pause");
return 0;
}
输出结果:
the position of 'heat_nan' is 19
the position of the first 'one' is 9
the position of the second 'one' is 38
hi,*very one! I am heat_nan from dlut one
参考
https://www.cnblogs.com/catgatp/p/6407788.html
https://blog.csdn.net/iot_change/article/details/8496977
string截取、替换、查找子串函数,find_first_of 用法的更多相关文章
- Oracle 截取、查找字符函数(持续更新)
整理一些常用的截取.查找字符函数: 1.查找某一个字符串中某一个字符(串)出现的次数 SELECT LENGTH(REGEXP_REPLACE(REPLACE('anne<br>lily& ...
- C++基础-string截取、替换、查找子串函数
1. 截取子串 s.substr(pos, n) 截取s中从pos开始(包括0)的n个字符的子串,并返回 s.substr(pos) 截取s中从从pos开始(包括0)到末尾的所有字 ...
- String字符串操作--切割,截取,替换,查找,比较,去空格.....
字符串拼接 直接用+号:String a = "I"; String b = "love"; String c = "you";String ...
- python中string模块各属性以及函数的用法
任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作. python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串 ...
- 牛人总结python中string模块各属性以及函数的用法,果断转了,好东西
http://blog.chinaunix.net/uid-25992400-id-3283846.html http://blog.csdn.net/xiaoxiaoniaoer1/article/ ...
- [转]Python 字符串操作实现代码(截取/替换/查找/分割)
原文地址:http://www.jb51.net/article/38102.htm ps:好久没更新python代码了,这次用到了字符串,转来看看 Python 截取字符串使用 变量[头下标:尾下标 ...
- Python 字符串操作(截取/替换/查找/分割)
Python 截取字符串使用 变量[头下标:尾下标],就可以截取相应的字符串,其中下标是从0开始算起,可以是正数或负数,下标可以为空表示取到头或尾. # 例1:字符串截取 str = '1234567 ...
- python查找字符串 函数find() 用法
sStr1 = 'abcdefg' sStr2 = 'cde' print sStr1.find(sStr2) 输出 2意思是在sStr1字符里的第2位置找到了包含cde字符的字段
- Python中字符串String的基本内置函数与过滤字符模块函数的基本用法
Python中字符串String的基本内置函数与用法 首先我们要明白在python中当字符编码为:UTF-8时,中文在字符串中的占位为3个字节,其余字符为一个字节 下面就直接介绍几种python中字符 ...
随机推荐
- 现代汽车加入Linux 基金会和 AGL协作平台
1月4日,现代汽车宣布已加入 Linux 基金会和其旗下的非营利协作平台 Automotive Grade Linux(AGL),现代汽车公司副总裁兼信息娱乐技术中心负责人 Paul Choo 表示: ...
- vue打包报内存溢出
vue-cli 构建的项目:package.json 文件里修改: "build": "node build/build.js" 修改为: "buil ...
- mysql引擎和事务
对于应用程序和用户来说,同样一张表的数据无论用什么引擎来存储,看到的数据都是一样的,只是不同的引擎在功能.占用空间大小.读取性能等方面可能有所差别. mysql最常用的存储引擎为Innodb.MyIS ...
- dubbo spring pom文件报错:提示no declaration can be found for element 'dubbo:service'.
pom文件报错:The matching wildcard is strict, but no declaration can be found for element 'dubbo:service ...
- rabbitmq heartbeat missing with heartbeat = N seconds原因总结
一直以来,在我们大规模使用rabbitmq的服务端应用中,都没有出现rabbitmq心跳超时而造成的的影响,反倒是在rabbitmq-cpp客户端出现过很多次该问题,一直以为客户端lib实现的问题(不 ...
- MacOS Docker 安装
使用 Homebrew 安装 macOS 我们可以使用 Homebrew 来安装 Docker. Homebrew 的 Cask 已经支持 Docker for Mac,因此可以很方便的使用 Home ...
- sentos7为例添加python3和python2共存
转载:https://www.cnblogs.com/JahanGu/p/7452527.html 1.查看是否已经安装Python CentOS 7.2 默认安装了python2.7.5 因为一些命 ...
- 盒子总结,文本属性操作,reset操作,高级选择器,高级选择器优先级,边界圆角(了解),a标签的四大伪类,背景图片操作,背景图片之精灵图
盒子总结 ''' block: 设置宽高 1.没有设置宽,宽自适应父级的宽(子级的border+padding+width=父级的width) 2.没有设置高,高由内容撑开 设置了宽高 一定采用设置的 ...
- 复旦高等代数II(16级)每周一题
每周一题的说明 一.本学期高代II的每周一题面向16级的同学,将定期更新(一般每周的周末公布下一周的题目); 二.欢迎16级的同学通过微信或书面方式提供解答图片或纸质文件给我,优秀的解答可以分享给大家 ...
- ScheduledTheadPool线程池的使用
ScheduledTheadPool线程池的特点在于可以延迟执行任务,也可以周期性执行任务. 创建线程池 ScheduledExecutorService scheduled = Executors. ...