源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

浅谈C++中replace()方法

  • 时间:2020-10-31 02:25 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:浅谈C++中replace()方法
本文主要针对c++中常用replace函数用法给出九个样例程序: [b]用法一:[/b] 
/*
 *用str替换指定字符串从起始位置pos开始长度为len的字符 
 *string& replace (size_t pos, size_t len, const string& str); 
 */ 
int main() 
{ 
 string line = "this@ is@ a test string!"; 
 line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空 
 cout << line << endl; 
 return 0; 
} 
运行结果: [img]http://files.jb51.net/file_images/article/201511/20151110133108182.png?20151010133120[/img] [b]用法二:[/b] 
/*
 *用str替换 迭代器起始位置 和 结束位置 的字符 
 *string& replace (const_iterator i1, const_iterator i2, const string& str); 
 */ 
int main() 
{ 
 string line = "this@ is@ a test string!"; 
 line = line.replace(line.begin(), line.begin()+6, ""); //用str替换从begin位置开始的6个字符 
 cout << line << endl; 
 return 0; 
} 
运行结果: [img]http://files.jb51.net/file_images/article/201511/20151110133132159.png?20151010133140[/img] [b]用法三: [/b]
/*
 *用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串 
 *string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); 
 */ 
int main() 
{ 
 string line = "this@ is@ a test string!"; 
 string substr = "12345"; 
 line = line.replace(0, 5, substr, substr.find("1"), 3); //用substr的指定子串(从1位置数共3个字符)替换从0到5位置上的line 
 cout << line << endl; 
 return 0; 
} 
运行结果: [img]http://files.jb51.net/file_images/article/201511/20151110133203827.png?20151010133212[/img] [b]用法四:[/b]string转char*时编译器可能会报出警告,不建议这样做 
/*
 *用str替换从指定位置0开始长度为5的字符串 
 *string& replace(size_t pos, size_t len, const char* s); 
 */ 
int main() 
{ 
 string line = "this@ is@ a test string!"; 
 char* str = "12345"; 
 line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串 
 cout << line << endl; 
 return 0; 
} 


运行结果: [img]http://files.jb51.net/file_images/article/201511/20151110133222801.png?20151010133238[/img] [b]用法五:[/b]string转char*时编译器可能会报出警告,不建议这样做 
/*
 *用str替换从指定迭代器位置的字符串 
 *string& replace (const_iterator i1, const_iterator i2, const char* s); 
 */ 
int main() 
{ 
 string line = "this@ is@ a test string!"; 
 char* str = "12345"; 
 line = line.replace(line.begin(), line.begin()+9, str); //用str替换从指定迭代器位置的字符串 
 cout << line << endl; 
 return 0; 
} 
运行结果: [img]http://files.jb51.net/file_images/article/201511/20151110133312784.png?20151010133323[/img] [b]用法六:[/b]string转char*时编译器可能会报出警告,不建议这样做 
/*
 *用s的前n个字符替换从开始位置pos长度为len的字符串 
 *string& replace(size_t pos, size_t len, const char* s, size_t n); 
 */ 
int main() 
{ 
 string line = "this@ is@ a test string!"; 
 char* str = "12345"; 
 line = line.replace(0, 9, str, 4); //用str的前4个字符替换从0位置开始长度为9的字符串 
 cout << line << endl; 
 return 0; 
} 
运行结果: [img]http://files.jb51.net/file_images/article/201511/20151110133334376.png?20151010133342[/img] [b]用法七:[/b]string转char*时编译器可能会报出警告,不建议这样做 
/*
 *用s的前n个字符替换指定迭代器位置(从i1到i2)的字符串 
 *string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n); 
 */ 
int main() 
{ 
 string line = "this@ is@ a test string!"; 
 char* str = "12345"; 
 line = line.replace(line.begin(), line.begin()+9, str, 4); //用str的前4个字符替换指定迭代器位置的字符串 
 cout << line << endl; 
 return 0; 
} 
运行结果: [img]http://files.jb51.net/file_images/article/201511/20151110133352924.png?2015101013345[/img] [b]用法八:[/b] 
/* 

*用重复n次的c字符替换从指定位置pos长度为len的内容 
 *string& replace (size_t pos, size_t len, size_t n, char c); 
 */ 
int main() 
{ 
 string line = "this@ is@ a test string!"; 
 char c = '1'; 
 line = line.replace(0, 9, 3, c); //用重复3次的c字符替换从指定位置0长度为9的内容 
 cout << line << endl; 
 return 0; 
} 

运行结果: [img]http://files.jb51.net/file_images/article/201511/20151110133415680.png?20151010133424[/img] [b]用法九: [/b]
/*
 *用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容 
 *string& replace (const_iterator i1, const_iterator i2, size_t n, char c); 
 */ 
int main() 
{ 
 string line = "this@ is@ a test string!"; 
 char c = '1'; 
 line = line.replace(line.begin(), line.begin()+9, 3, c); //用重复3次的c字符替换从指定迭代器位置的内容 
 cout << line << endl; 
 return 0; 
} 


运行结果: [img]http://files.jb51.net/file_images/article/201511/20151110133444660.png?2015101013355[/img] 注:所有使用迭代器类型的参数不限于string类型,可以为vector、list等其他类型迭代器。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部