C++ 中字符串查找、字符串截取、字符串替换

2023-05-16

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 str2
	iPos = str1.find_first_of(str2, 0);
	cout << "Element in '" << str2.c_str() << "' found at position " << iPos << endl;
 
	//start looking in the third position
	iPos = 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 position
	char 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 elements
	char 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 position
	iPos = 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类型的字符串

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++ 中字符串查找、字符串截取、字符串替换 的相关文章

随机推荐

  • Linux从用户层到内核层系列 - GNU系列之你所不知道的printf

    题记 xff1a 本系列文章的目的是抛开书本从源代码和使用的角度分析Linux内核和相关源代码 xff0c byhankswang和你一起玩转linux开发 轻松搞定TCP IP协议栈 xff0c 原创文章欢迎交流 byhankswang
  • 第十一届蓝桥杯python组第二场省赛-排序

    1 问题描述 xff1a 小蓝最近学习了一些排序算法 xff0c 其中冒泡排序让他印象深刻 在冒泡排序中 xff0c 每次只能交换相邻的两个元素 小蓝发现 xff0c 如果对一个字符串中的字符排序 xff0c 只允许交换相邻的两个字符 xf
  • 4306 序列处理(贪心)

    1 问题描述 xff1a 给定一个长度为 n 的整数序列 a1 xff0c a2 xff0c xff0c an 我们可以对该序列进行修改操作 xff0c 每次操作选中其中一个元素 xff0c 并使其增加 1 现在 xff0c 请你计算要使得
  • python数据分析基础-datafrom基础属性

    linux 43 pycharm 43 anaconda span class token keyword import span pandas span class token keyword as span pd data1 span
  • python数据分析基础-series基本统计运算,数据运算和比较运算

    linux 43 pycharm 43 anaconda 96 span class token comment 两个series数据运算 43 span num span class token operator 61 span pd s
  • python数据分析-柱状图绘制及常用参数设置

    linux 43 pycharm 43 anaconda span class token comment 柱形图绘制与参数设置 span span class token comment plt bar x height width bo
  • 爬取西窗烛app

    本文旨在学习 xff0c 不可做商业用途 span class token keyword import span csv span class token keyword import span json span class token
  • 爬取百度地图店家信息

    本文旨在交流学习 xff0c 勿作他用 xff0c 否则后果自负 span class token keyword import span json span class token keyword import span csv span
  • 爬取蝉妈妈数据平台商品数据

    本文旨在交流学习 xff0c 勿作他用 xff0c 否则后果自负 环境 linux 43 pycharm 43 anaconda span class token keyword import span json span class to
  • selenium+webdriver+chrome实现百度以图搜图

    1 环境搭建 1 webdriver 43 chrome的版本需匹配 2 打开chrome 输入 chrome version 来查看chrome版本 访问此网站 谷歌浏览器驱动 然后选择合适版本webdriver 2 收集原始图片和构建图
  • 无聊之重学c/c++

    span class token macro property span class token directive hash span span class token directive keyword include span spa
  • Python配置Pip国内源

    临时指定下载源 以阿里云的镜像为例 pip install trusted host https mirrors aliyun com i https mirrors aliyun com pypi simple lt some packa
  • 爬虫效率提升方法

    协程 xff1a 在函数 特殊函数 定义的时候 使用async修饰 函数调用后 内部语句不会立即执行 而是会返回一个协程对象 任务对象 xff1a 任务对象 61 高级的协程对象 进一步封装 61 特殊的函数 xff0c 任务对象必须要注册
  • selenium JD爬虫

    python3 7 43 win10 span class token comment coding utf 8 span span class token comment 64 Time 2022 6 27 16 55 span span
  • 树莓派4b学习笔记三:三行命令极简安装vscode

    很多写代码的软件在树莓派上都不能用 xff0c 或者安装比较麻烦 恰巧 xff0c vscode作为一款轻量级代码编辑器 xff0c 可以在树莓上使用 xff0c 而且它通过简单的安装插件就可以支持很多种编程语言 我在网上搜了很多关于在树莓
  • you-get详细配置教程

    一 you get介绍 you get是一款能够直接从网页上下载视频 音乐 图片的命令行多媒体下载器 xff0c 它支持80多个网站 xff0c 包括某酷 某奇艺 某讯 Bzhan YouTube等 xff0c 具体说明及支持网站可点击下方
  • hyper-v虚拟机上的ubuntu 18.04 LTS通过xrdp传递声音遇到的一些坑

    背景 xff1a Windows10 pro上hyper v自带的ubuntu 18 04 LTS 在使用虚拟机时发现没有声音 xff0c 通过这篇博客Linux xrdp远程桌面连接声音重定向来到 pulseaudio module xr
  • 深入理解卡尔曼滤波算法

    最近做卡尔曼滤波跟踪的项目 xff0c 看原理花了一天 xff0c 再网上查找并看懂别人的kalman c 43 43 代码花了我近三天的时间 卡尔曼滤波就是纸老虎 xff0c 核心原理不难 xff0c 核心公式就5个 xff0c 2个状态
  • no module named ‘apex‘ 的解决方案

    背景 xff1a 部署fast reid工程环境的时候报出ModuleNotFoundError No module named 39 apex 的错误 纯粹记录一下 no module named apex 的解决方案 xff1a 1 官
  • C++ 中字符串查找、字符串截取、字符串替换

    1 字符串查找 s span class token punctuation span span class token function find span span class token punctuation span s1 spa