C++实现字符串替换的两种方法
2013-06-09 13:43
507 查看
替换字符串replace() erase()
//C++ 第一种替换字符串的方法用replace()|C++ 第二种替换字符串的方法用erase()和insert()【 C++[b]string|C++ replace()|C++ erase()|C++ insert()|C++自定义替换字符串函数】 [/b] #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; } }
相关文章推荐
- C++实现字符串替换的两种方法
- 【C/C++开发】C++实现字符串替换的两种方法
- C++实现字符串替换的两种方法
- C++字符串替换函数具体实现方法详解
- "找出字符串中第一个不重复的字符"和"找出字符串中最后一个重复的字符"的两种方法的C++实现
- C++实现将一个字符串中的字符替换成另一个字符串的方法
- c/c++ 使用boost库实现的一个简单判断字符串编码的方法
- 实现字符串倒序的两种方法
- C++ 分割字符串两种方法
- 替换字符串中的空格(C++/Java实现)
- c++的字符串char与string相互转化,以及string的,切割,替换字符,字符串拼接方法(object向c++转型系列教程2)
- c语言:两种方法实现字符串拷贝strcpy
- C++实现字符串替换
- 浮点性(float)转化为字符串类型 自定义实现和深入探讨C++内部实现方法
- PHP开发小技巧④—两种方法实现获取随机字符串
- 两种方法实现右旋字符串中的k个字符
- C++输入一个字符串,把其中的字符按照逆序输出的两种方法
- c语言:两种方法实现字符串拷贝strcpy
- @V@ java代码笔记2010-06-12:java控制台输入各类型类实现;以及判断输入字符串里面是否有数字的两种方法:方法1:转换成字符数组;方法2:正则表达式。
- 【旧资料整理】c\c++两种定义字符串数组方法内存开销比较