【C/C++开发】C++实现字符串替换的两种方法
生活随笔
收集整理的這篇文章主要介紹了
【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;}
}
轉載于:https://www.cnblogs.com/huty/p/8517186.html
總結
以上是生活随笔為你收集整理的【C/C++开发】C++实现字符串替换的两种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017年读书计划(一)
- 下一篇: vc调试大全