C++ 中字符串查找、字符串截取、字符串替换
1、字符串查找
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)
字符串截取
s.substr(pos, n) //截取s中从pos开始(包括0)的n个字符的子串,并返回s.substr(pos) //截取s中从从pos开始(包括0)到末尾的所有字符的子串,并返回
字符串替换
s.replace(pos, n, s1) //用s1替换s中从pos开始(包括0)的n个字符的子串
代码测试(字符串操作.cpp)
#include <iostream>
using namespace std;/* 字符串查找 */
void findSubString(string str){// find()函数的使用,返回查找对象第一次出现的位置. cout << str.find("fs") << endl;// rfind()函数的使用,返回查找对象最后出现的位置cout << str.rfind("s") << endl;
}/* 字符串截取 */
void getSubString(string str){// substr(pos)函数的使用,返回从pos开始(包含pos位置的字符)所有的字符cout << str.substr(2) << endl;// substr(pos,n),返回从pos开始(包含pos位置的字符)n个字符cout << str.substr(2, 2) << endl;
}/* 字符串替换 */
void replaceString(string str){// replace(pos,n,s1),用s1替换从pos开始的n个字符cout << str.replace(0,2,"xiaoming") << endl;
}int main()
{string str = string("sdfsf");// findSubString(str);// getSubString(str);replaceString(str);return 0;
}
字符替换(用x替换字符串中所有的a.cpp)
#include <iostream>
using namespace std;/* 用x替换a */
void replaceAWithX(string str){int pos;pos = str.find("a");while(pos != -1){// str.length()求字符的长度,注意str必须是string类型str.replace(pos,string("a").length(),"x");pos = str.find("a");}cout << str << endl;
}int main()
{string str = string("fsafsdf");replaceAWithX(str);return 0;
}
C++ string类中的字符串查找
类string提供了大量查找功能和搜索功能,其中比较常用的查找和搜索函数是find()函数、
find_first_not_of()函数、find_first_of()函数、find_last_not_of()函数、find_last_of()函数、rfind()等。
find()函数的语法如下所示:
(1) size_type find(E c, size_type pos = npos) const;用来查找单个字符在字符串中出现的位置并返回该位置基于0的索引值。
(2) size_type find(const E *s, size_type pos = npos) const;用来查找以空字符结尾的字符数组在字符串中出现的位置并返回该位置基于0索引值。
(3) size_type find(const E *s, size_type pos, size_type n = npos) const;用来查找以空字符结尾的字符数组在字符串中出现的位置并返回该位置基于0索引值,它是从npos开始查找的。
(4) size_type find(const basic_string &str, size_type pos = npos) const;用来查找字符串并且返回该搜索的字符串的基于0索引值的位置值,它是从npos开始查找的。find()函数的功能是从std::string对象的头部顺序找目标值,如果找到返回该目标值出现的位置,如果没有在字符串对象中找到目标对象,返回值为-1。rfind()函数的语法如下所示:
(1) size_type rfind(E c, size_type pos = npos) const;用来反向查找单个字符在字符串中出现的位置并返回该位置基于0的索引值。
(2) size_type rfind(const E *s, size_type pos = npos) const;用来反向查找以空字符结尾的字符数组在字符串中出现的位置并返回该位置基于0索引值。
(3) size_type rfind(const E *s, size_type pos, size_type n = npos) const;用来反向查找以空字符结尾的字符数组在字符串中出现的位置并返回该位置基于0索引值,它是从npos开始查找的。
(4) size_type rfind(const basic_string &str, size_type pos = npos) const;用来反向查找字符串并且返回出现该搜索的字符串的基于0索引值的位置值,它是从npos开始查找的。
rfind()函数的功能是从std::sring对象的尾部反向查找目标值,如果找到返回该目标值出现的位置,如果没有在字符串对象中找到目标对象,返回值为-1。find_first_not_of()函数的常见语法如下所示:
size_type find_first_not_of(E c, size_type pos = 0) const;
size_type find_first_not_of(const E *s, size_type pos = 0) const;
size_type find_first_not_of(const E *s, size_type pos, size_type n) const;
size_type find_first_not_of(const basic_string &str, size_type pos = 0) const;
该函数的功能是在string对象中查找对象,如果在string出现了完全不匹配的字符,字符串或以空字符结尾的字符数组时,系统显示第一次出现这种情形的位置。如果定义了pos,从pos开始搜索。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;
该函数的功能是在string对象中查找对象,如果在string出现了任意完全匹配的字符,字符串或以空字符结尾的字符数组时,系统显示第一次出现这种情形的位置。如果定义了pos,从pos开始搜索。只要在string对象中出现了匹配对象,立即返回位置。find_last_not_of()函数的常见语法如下所示:
size_t find_last_not_of( const string& str, size_t pos = npos ) const;
size_t find_last_not_of( const char* s, size_t pos, size_t n ) const;
size_t find_last_not_of( const char* s, size_t pos = npos ) const;
size_t find_last_not_of( char c, size_t pos = npos ) const;
该函数的功能是在string对象中反向查找对象,如果在string出现了任意完全不匹配的字符,字符串或以空字符结尾的字符数组时,系统显示第一次完全不匹配时出现的位置。如果定义了pos,从pos反向开始搜索。只要在string对象中出现了完全不匹配的对象,立即返回位置值。find_last_of()函数的常见语法如下所示:
size_t find_last_of( const string& str, size_t pos = npos ) const;
size_t find_last_of( const char* s, size_t pos, size_t n ) const;
size_t find_last_of( const char* s, size_t pos = npos ) const;
size_t find_last_of( char c, size_t pos = npos ) const;
该函数的共是在string对象中反向查找对象,如果在string出现了任意完全匹配的字符,字符串或以空字符结尾的字符数组时,系统显示第一次出现这种情形的位置。如果定义了pos,从pos开始反向搜索。只要在string对象中出现了匹配的对象,则立即返回位置值。
#include <iostream>
#include <string>using namespace std;int main(int argc, char *argv[])
{string str1("Heartbeat");string str2("abcde");int iPos = 0;cout << "The string to search is '" << str1.c_str() << "'" << endl;//find the first instance in str1 of any characters in str2iPos = str1.find_first_of(str2, 0);cout << "Element in '" << str2.c_str() << "' found at position " << iPos << endl;//start looking in the third positioniPos = str1.find_first_of(str2, 2);cout << "Element in '" << str2.c_str() << "' found at position " << iPos << endl;//use an array of the element type as the set of elements to search for;//look for anything after the fourth positionchar achVowels[] = {'a', 'e', 'i', 'o', 'u'};iPos = str1.find_first_of(achVowels, 4, sizeof(achVowels));cout << "Element in '";for (int i = 0; i < sizeof(achVowels); ++i){cout << achVowels[i];}cout << "' found at position " << iPos << endl;//use a string literal to specify the set of elementschar szVowels[] = "aeiou";iPos = str1.find_first_of(szVowels, 0);cout << "Element in '" << szVowels << "' found at position " << iPos << endl;//look for a specific character beginning in the third positioniPos = str1.find_first_of('e', 2);cout << "'e' found at position " << iPos << endl;return 0;
}
运行结果为:
The string to search is ‘Heartbeat’
Element in ‘abcde’ found at position 1
Element in ‘abcde’ found at position 2
Element in ‘aeiou’ found at position 6
Element in ‘aeiou’ found at position 1
‘e’ found at position 6
附:
stoi函数: 将string类型转换成int类型的函数
stod函数: 将string类型转换成double类型的函数
atof函数: 将string类型转换成double类型的函数
stoi - C++ Reference (cplusplus.com)
stod - C++ Reference (cplusplus.com)
atof - C++ Reference (cplusplus.com)
两个函数的共同特性:
1.会自动截取所需要的类型数值
2.遇到非数字,截取停止,即使后面有数字也不会继续读取了
atof函数的个性:
1.未找到时返回值为0
2.stod函数使用对象为string类型,而atof函数为const char*
stoi/stod函数适应性较好,可以读取string类型的字符串
C++ 中字符串查找、字符串截取、字符串替换相关推荐
- python字符串截取拼接_Python字符串拼接、截取及替换方法总结分析
本文实例讲述了Python字符串拼接.截取及替换方法.分享给大家供大家参考,具体如下: python字符串连接 python字符串连接有几种方法,我开始用的第一个方法效率是最低的,后来看了书以后就用了 ...
- ASP如何计算中英文混合字符串长度和截取字符串
用ASP做网站的时候经常会碰到要截取字符串的情况.ASP中的Len函数不管是中文字符,还是英文字符,统统按一个单位来计算,由于一个中文字符的宽度是一个英文字符宽度的两倍,在中英文混合的情况下字符串实际 ...
- python中字符串切片取奇数_Python中的字符串切片(截取字符串)的详解
Python中的字符串切片(截取字符串)的详解 字符串索引示意图 字符串切片也就是截取字符串,取子串 Python中字符串切片方法 字符串[开始索引:结束索引:步长] 切取字符串为开始索引到结束索引- ...
- CString字符串查找和截取
一.CString之Find().FindOneOf().ReverseFind().此三个函数返回值均为整数int. 1.Find() 该函数从左侧0索引开始,查找第一个出现的字符位置,返回posi ...
- CString字符串查找和截取与去空格(Find,FindOneOf,ReverseFind,Left,Mid,Right,substr,TrimLeft和TrimRight)
一.CString之Find().FindOneOf().ReverseFind(). 此三个函数返回值均为整数int. 1.Find() 该函数从最左侧0位置开始索引,查找到第一个出现该字符的位置, ...
- Js中substr,substring,slice截取字符串的异同
概述 今天在写程序的时候发现js中slice也能用来截取字符串,以前都是用substr或substring,于是想着拿它们来对比一下,它们都可以接受两个参数,第一个是开始截取的位置,默认为0,第二个下 ...
- python中字符串查找子串_Python字符串中查找子串的方法
Python字符串中查找子串的方法 发布于 2015-04-12 08:58:32 | 230 次阅读 | 评论: 0 | 来源: 网友投递 Python编程语言Python 是一种面向对象.解释型计 ...
- js中根据特定字符截取字符串
1,indexOf()根据字符确定位置 2,substring()根据位置截取字符串. 举例:pLLTude:28.109069|116.218152 ,根据 | 截取它前后得纬度和经度 pLLTud ...
- vue截取一个字符串_vue如何截取字符串
在后端有许多的封装方法来截取字符串或者对字符串的操作,同样前端也有相应的方法. 有一个data数据为ipaddr data() { return { ipaddr: "192.168.100 ...
- iOS开发:字符串处理:截取字符串、匹配字符串、分割字符串
分享一个比较基础的知识点,记录一下,以备以后使用的时候查找方便,现在岁数大了,脑子不好使了,记忆力减退,年轻人可以忽略此博客.本篇要分享的是关于iOS开发中,字符串简单处理的方法,此部分过于基础,仅作 ...
最新文章
- linux下安装erlang
- transformer bert GPT(未完)
- 【转】AndroidStudio升到最新版本(3.1.2)之后
- echarter: ECharts的R语言接口(一)
- Java类权限和类成员权限举例解析
- 计算机安全关联的软件,紫鸟超级浏览器亚马逊账号防关联工具
- SCI写作攻略——附带常见英语写作句式
- Unity物理引擎基础优化准则
- Dreamweaver cs6 网页设计教程笔记
- 手机、PDA、车载GPS导航入门手册
- 微信公众号的代码块插入,及一键排版
- C语言递归(pta递归求简单交错幂级数的部分和)
- 三相永磁同步电机无速度传感器控制(基于扩展反电动势)
- Spring Cloud与Docker微服务架构实战 PDF版 内含目录
- hg和git命令对照表
- 解决:电脑主机一会儿自动关闭又自动启动又自动关闭...这样循环往复怎么办?主机用着用着突然关机怎么回事?
- 手工打造C#IDE环境(一):万事开头难
- 2018-07-13心情日记
- 增强版 Git Flow 模型
- 操作系统之进程的同步机制