def myReplace(s,sub, dest, times =None):
#如果times是None,替换的次数是s.count(sub)
if times == None:
times = s.count(sub)
sub_index = []
sub_length = len(sub)
dest_length = len(dest)
s = list(s)
for i in range(len(s)):
if s[i:i+sub_length] == list(sub):
sub_index.append(i) n = 0
for index in sub_index:
if times > 0:
offset = n * (dest_length - sub_length)
index = index + offset
s[index:index+sub_length] = list(dest)
n += 1
times -= 1
return "".join(s) print(myReplace("abcc1aha",'a','xy'))
print(myReplace("abcdefgabca","a","",1))

20191213用Python实现replace方法的更多相关文章

  1. python字符串replace()方法

    python字符串replace()方法 >>> help(str.replace)Help on method_descriptor:replace(...)    S.repla ...

  2. Python string replace 方法

    Python string replace   方法 方法1: >>> a='...fuck...the....world............' >>> b=a ...

  3. Python之replace()方法失效

    1.背景 Titanic存活率预测案例: # 读取数据 df_train = pd.read_csv("./data/train.csv") df_train.head() OUT ...

  4. Python replace()方法

    描述 Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次. 语法 replace()方法语法: st ...

  5. python中的replace()方法的使用

    python中的replace()方法的使用 需求是这样的:需要将字符串的某些字符替换成其他字符 str.replace(old,new,max) 第一个参数是要进行更换的旧字符,第二个参数是新的子串 ...

  6. Python中的replace方法

    replace 方法:返回根据正则表达式进行文字替换后的字符串的复制. stringObj.replace(rgExp, replaceText) 参数 stringObj必选项.要执行该替换的 St ...

  7. Python replace方法并不改变原字符串

    直接给出结论:replace方法不会改变原字符串. temp_str = 'this is a test' print(temp_str.replace('is','IS') print(temp_s ...

  8. Python数据类型及其方法详解

    Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...

  9. python字符串的方法

    python字符串的方法 ############7个基本方法############ 1:join def join(self, ab=None, pq=None, rs=None): # real ...

随机推荐

  1. 测开之路一百五十三:ajax之load、get、ajax在项目中的体现

    在查询的时候是使用ajax进行请求的 目录结构 personal.models from datetime import datetimefrom flask_sqlalchemy import SQ ...

  2. Lesson 1 A puma at large

    spot (v) 看出,发现 oblige (v) 使...感到必须:obliged (adj)必须的, feel obliged to do sth. 感到不得不做某事 ==have to.eg:E ...

  3. java正则匹配正则表达式

    1.简单匹配小案例 public static void main( String[] args ){ // 按指定模式在字符串查找 String line = "This order wa ...

  4. 【Qt开发】QT样式表单 qss的样式优化

    QT样式表单 QT的样式表单允许我们在对程序不做任何代码上的更改的情况下轻松改变应用程序的外观. 其思想来源于网页设计中的CSS,即可以将功能设计和美学设计分开. 它的语法和概念和HTML CSS也是 ...

  5. C/C++ 防止头文件重复包含 #pragma once 与 #ifndef 的区别

    为了避免同一个头文件被多重包含/重复包含,有两种方式: 方式一: #ifndef XXX #define XXX ... ...  //声明语句 #endif  //XXX 方式二: #pragma ...

  6. c++ Oracle OCCI 编程

    转载备忘:http://blog.sina.com.cn/s/blog_53a72add01015zj4.html 关于occi编程可以参考的链接: http://blog.itpub.net/162 ...

  7. while与格式化的练习

    练习 判断下列逻辑语句的结果,一定要自己先分析 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 2)n ...

  8. Node+Express+MySql实现简单增删改查和登录

    var express = require('express'); var mysql = require('mysql'); var app = express(); var bodyParser ...

  9. 阿里云服务器重启出现An error occurred 如何处理

    最近网站重启阿里云服务后,出现 An error occurred, An error occurred. Sorry, the page you are looking for is current ...

  10. 一般软件开发流程和BBS表设计

    项目开发流程 需求分析 架构师+产品经理+开发组组长 和客户公司谈需求之前 ,事先需要想一下这个项目要怎么做 里面的坑点提前想好比较简单的解决方案 在跟客户谈的时候有意识的引导客户朝你已经想好的方案上 ...