zoukankan      html  css  js  c++  java
  • 【C/C++开发】C++实现字符串替换的两种方法

    替换字符串replace() erase()
    //C++ 第一种替换字符串的方法用replace()|C++ 第二种替换字符串的方法用erase()和insert()【 C++string|C++ replace()|C++ erase()|C++ insert()|C++自定义替换字符串函数】
    
    
    #include<string>
    #include<iostream>
    using namespace std;
    
    //第一种替换字符串的方法用replace()
    
    void string_replace(string&s1,const string&s2,const string&s3)
    {
    	string::size_type pos=0;
    	string::size_type a=s2.size();
    	string::size_type b=s3.size();
    	while((pos=s1.find(s2,pos))!=string::npos)
    	{
    		s1.replace(pos,a,s3);
    		pos+=b;
    	}
    }
    
    //第二种替换字符串的方法用erase()和insert()
    
    void string_replace_2(string&s1,const string&s2,const string&s3)
    {
    	string::size_type pos=0;
    	string::size_type a=s2.size();
    	string::size_type b=s3.size();
    	while((pos=s1.find(s2,pos))!=string::npos)
    	{
    		s1.erase(pos,a);
    		s1.insert(pos,s3);
    		pos+=b;
    	}
    }
  • 相关阅读:
    linux常用命令笔记
    head first html与css
    多线程编程核心技术日记
    nio
    排序算法
    随笔
    数据库读写分离
    购物网站设计
    http
    servlet初始化
  • 原文地址:https://www.cnblogs.com/huty/p/8517187.html
Copyright © 2011-2022 走看看