How to subtract two strings?

后端 未结 8 2195
执笔经年
执笔经年 2020-12-30 00:53

I have a long string, which is basically a list like str=\"lamp, bag, mirror,\" (and other items)

I was wondering if I can add or subtract some items, i

8条回答
  •  情深已故
    2020-12-30 01:21

    from re import sub
    
    def Str2MinusStr1 (str1, str2, n=1) :
        return sub(r'%s' % (str2), '', str1, n)
    
    Str2MinusStr1 ('aabbaa', 'a')  
    # result 'abbaa'
    
    Str2MinusStr1 ('aabbaa', 'ab')  
    # result 'abaa'
    
    Str2MinusStr1 ('aabbaa', 'a', 0)  
    # result 'bb'
    
    # n = number of occurences. 
    # 0 means all, else means n of occurences. 
    # str2 can be any regular expression. 
    

提交回复
热议问题