#include <iostream>

#include <string>

using namespace std;

/*

*  函数功能:将string字符串中的某些字符替换成其他字符

*  参数说明:  str 原字符串  strFind 需要替换字符串   strReplace 替换字符串

*/

string replace(string &str,const string &strFind,const string &strReplace)

{

int fsize = strFind.size();

int rsize = strReplace.size();

int pos = str.find(strFind,0);//从开始位置查找第一个符合的字符串,未找到返回str.npos

while (pos!=str.npos)

{

str.replace(pos,rsize,strReplace); //替换查找到的字符串

pos = str.find(strFind,pos+fsize); //继续查找

}

return str;

}

int main(int argc, char* argv[])

{

string str="abc[def[gef[ee[";

string strFind ="[";

string strReplace="(";

replace(str,strFind,strReplace);  //将字符串str中的“[”替换为“(”

cout<<str<<endl;

return 0;

}

string类自定义字符串替换函数replace的更多相关文章

  1. JS字符串替换函数:Replace(“字符串1″, “字符串2″),

    JS字符串替换函数:Replace(“字符串1″, “字符串2″), 1.我们都知道JS中字符串替换函数是Replace(“字符串1″, “字符串2″),但是这个函数只能将第一次出现的字符串1替换掉, ...

  2. sql server 字符串替换函数REPLACE

    sql server 字符串替换函数REPLACE函数的使用 <pre name="code" class="sql">--参数1:需要替换字符的母 ...

  3. Java字符串替换函数replace、replaceFirst、replaceAll

    一.replace(String old,String new) 功能:将字符串中的所有old子字符串替换成new字符串 示例 String s="Hollow world!"; ...

  4. mysql 替换函数replace()实现mysql 替换字符串

    mysql 替换字符串的实现方法:mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数去替换,用起来非常的方便,mysql 替换函数replace()Upd ...

  5. mysql 替换函数replace()实现mysql替换指定字段中的字符串

    mysql 替换字符串的实现方法: mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数去替换,用起来非常的方便. mysql 替换函数replace() ...

  6. Sqlite数据库字符串处理函数replace

    Sqlite 字符串处理函数replace官方说明: replace(X,Y,Z) The replace(X,Y,Z) function returns a string formed by sub ...

  7. Java的API及Object类、String类、字符串缓冲区

    Java 的API 1.1定义 API: Application(应用) Programming(程序) Interface(接口) Java API就是JDK中提供给开发者使用的类,这些类将底层的代 ...

  8. sql server 数据字符串替换函数

    sql server 替换函数 replace 函数参数 REPLACE(string_expression, string_pattern, string_replacement) 1.string ...

  9. C++学习36 string类和字符串

    C++大大增强了对字符串的支持,除了可以使用C风格的字符串,还可以使用内置的数据类型 string.string 类处理起字符串来会方便很多,完全可以代替C语言中的 char 数组或 char 指针. ...

随机推荐

  1. linux 系统相关命令

    说明:此篇以 Debian ( ubuntu16.04 ) 命令为例 1. tab键默认是不能自动补全命令 apt install bash-completion // 安装完成之后重启系统 2. 虚 ...

  2. makefile编写helloworld

    相信在unix下编程的没有不知道makefile的,刚开始学习unix平台 下的东西,了解了下makefile的制作,觉得有点东西可以记录下. 下面是一个极其简单的例子: 现在我要编译一个Hello ...

  3. python之路-------字符串与正則表達式

    1.1.#####去掉字符串中的转义符string.strip() print "hello\tworld\n" >>> word="\thello w ...

  4. spring boot actuator工作原理之http服务暴露源码分析

    spring boot actuator的官方文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/productio ...

  5. 3339: Rmq Problem

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1269  Solved: 665[Submit][Status][Discuss] Descripti ...

  6. 在Ubuntu下使用命令删除目录

    在Ubuntu命令行中用命令删除目录,现在在Linux系统中删除目录大致会用两个,rm和rmdir,rm命令删除目录很简单,不过很多人还是比较习惯用rmdir命令,如果操作的目录非空时就有点麻烦.这时 ...

  7. ACM-ICPC 2017 Asia Urumqi(第八场)

    A. Coins Alice and Bob are playing a simple game. They line up a row of nnn identical coins, all wit ...

  8. 关于vsphere的 许可证配置问题

    exsi未获得许可情况: exsi的许可证: vcenter server 未获许可: vcenter server的许可证: 写在最后: 无所不能的中国人,百度一下  许可证 就什么多有了,佩服,佩 ...

  9. .net Web获取域用户账号

    HttpContext.Current.Request.LogonUserIdentity.Name //可以获取出域账号 HttpContext.Current.Request.LogonUserI ...

  10. 运维派 企业面试题2 创建10个 "十个随机字母_test.html" 文件

    Linux运维必会的实战编程笔试题(19题) 企业面试题2: 使用for循环在/tmp/www目录下通过随机小写10个字母加固定字符串test批量创建10个html文件,名称例如为: --[root@ ...