字符串的查找与替换

发布时间:2024年01月20日
#include<iostream>
using namespace std;

// # 1、z字符串的查找
void test01(){
	
	string str1 = "abcdfggdf";
	// 被查对象.find("查询内容",索引起点); 
	// 从左往右查找,返回 第一个 与查询内容相同的索引
	 
	int pos = str1.find("df",3) ; // 找不到的情况会返回值为:-1 
	cout << "pos = " << pos <<endl;// 3
	
	 // rfind 和 find区别
	//  rfind从左往右查找   find从左往右查找,仅此而已	
	pos = str1.rfind("df");
	cout << "pos = " << pos <<endl;  // 7
	
} 


// # 2、替换
void test02(){
	
	string str1 ="abcdefg";
	str1.replace(1,3,"1111");
	
	// 从1号位置起3个字符,替换为 四个"1111" 
	cout << "str1 = "<< str1 << endl; 
	
	
} 

int main(){
	
	test01();
	test02();
	
	return 0;
}
文章来源:https://blog.csdn.net/weixin_45465041/article/details/135715985
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。