C++:replace实现字符替换
##C++ string中的replace函数详解
1.应⽤⼀:string &replace(size_t pos,size_t len,const &str)被替换位置(pos往后len个字符)
2.应⽤⼆:string &replace(size_t pos,size_tlen,const string &str,size_t subpos,size_t sublen)被替换位置(pos往后len长度),替换位置(subpos往后sublen长度)
3.应⽤三:string &replace(size_t pos,size_t len,const char* s) 插⼊C串
4.应⽤四:string &replace(size_t pos,size_t len,const char* cch,size_t n)插⼊C串前n个字符
5.应⽤五:string &replace(size_t pos,size_t len,size_t n,char c)在指定位置插⼊指定个c字符
6.应⽤六~应⽤九:(往后为迭代器操作)
string &replace(const_iterator it1,const_iterator it2,const string&str)
string &replace(const_iterator it1,const_iterator it2,const char* cch)
string &replace(const_iterator it1,const_iterator it2,const char* cch,size_t n)
string &replace(const_iterator it1,const_iterator it2,size_t n,char c)
使⽤应⽤五,实现对指定字符的替换。'_'替换为' '。
#include <iostream>
#include <string>
字符串replace函数using namespace std;
int main()
{
string abc("ff_00_0000");
cout << "old string:" << abc.c_str() << endl;
for(int i = 0; i < abc.size(); i++)
{
if(abc.at(i) == '_')
{
}
}
cout << "new string:" << abc.c_str() << endl;
return 0;
}
程序运⾏结果:
old string:ff_00_0000
new string:ff 00 0000

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。