美文网首页python3自写小工具
扩展python3的replace()方法:去掉字符串中的指定字

扩展python3的replace()方法:去掉字符串中的指定字

作者: AmanWang | 来源:发表于2020-09-03 08:50 被阅读0次

    传参说明

    myString:原字符串
    oldSubStr:要替换的子字符串
    newSubStr:替换后的子字符串
    count:要替换的次数,默认为-1(负数-全部替换,0-不替换,1-替换第1个,n-替换前n个)
    caseSensitive:1-区分大小写,其他-不区分大小写

    源码

    def myReplace(myString, oldSubStr, newSubStr, count=-1, caseSensitive='1'):
        # 处理传参,不合法的时候强制转换为需要的类型
        oldSubStr = str(oldSubStr)
        newSubStr = str(newSubStr)
        myString = str(myString)
        if not isinstance(count, int):
            count = -1
    
        # oldSubStr为空时不做替换,直接返回原字符串
        if oldSubStr == '':
            return myString
    
        # 区分大小写时,直接调用replace()方法处理
        if str(caseSensitive) == '1':
            myString = myString.replace(oldSubStr, newSubStr, count)
            return myString
    
        # 不区分大小写时,oldSubStr在myString中不存在
        # countSubString()方法见另外一篇文章:https://www.jianshu.com/p/c392d9c29baa
        tem = countSubString(oldSubStr, myString, '1')
        if not tem['flag']:
            return myString
    
        # 不区分大小写时,oldSubStr在myString中存在
        oldSubStr = oldSubStr.lower()
        if count == 0:
            return myString
        elif count < 0:
            for i in tem['index_start']:
                myString = oldSubStr.join(myString.split(myString[i:i + len(oldSubStr)]))
        else:
            for i in tem['index_start'][:count]:
                myString = oldSubStr.join(myString.split(myString[i:i + len(oldSubStr)]))
        myString = myString.replace(oldSubStr, newSubStr, count)
        return myString
    

    测试结果

    # 引用
    if __name__ == '__main__':
        print('结果1:', myReplace('China will be a STRONG country!', 'will', 'must',-1,'0'))
        print('结果2:', myReplace('China will be a STRONG country!', 'will', 'must',0,'0'))
        print('结果3:', myReplace('China will be a STRONG country!', 'strong', 'stronger',-1,'1'))
        print('结果4:', myReplace('China will be a STRONG country!', 'strong', 'stronger',-1,'0'))
        print('结果5:', myReplace('China will be a STRONG country!', 't', '-t-',1,'0'))
        print('结果6:', myReplace('China will be a STRONG country!', 't', '-t-',2,'0'))
        print('结果7:', myReplace('China will be a STRONG country!', '!', '',-1,'0'))
        print('结果8:', myReplace('China will be a STRONG country!', '', '',-1,'0'))
        print('结果9:', myReplace('China will be a STRONG country!', 'abc', '',-1,'0'))
        print('结果10:', myReplace('China will be a STRONG country!', ' ', '',-1,'0'))
        print('结果11:', myReplace('China will be a STRONG country!', ' ', '',2,'0'))
        print('结果12:', myReplace('China will be a STRONG country!', ' ', '',10,'0'))
    
    # 结果
    结果1: China must be a STRONG country!
    结果2: China will be a STRONG country!
    结果3: China will be a STRONG country!
    结果4: China will be a stronger country!
    结果5: China will be a S-t-RONG country!
    结果6: China will be a S-t-RONG coun-t-ry!
    结果7: China will be a STRONG country
    结果8: China will be a STRONG country!
    结果9: China will be a STRONG country!
    结果10: ChinawillbeaSTRONGcountry!
    结果11: Chinawillbe a STRONG country!
    结果12: ChinawillbeaSTRONGcountry!
    

    相关文章

      网友评论

        本文标题:扩展python3的replace()方法:去掉字符串中的指定字

        本文链接:https://www.haomeiwen.com/subject/vxhwsktx.html