Remove substring only at the end of string

后端 未结 11 705
再見小時候
再見小時候 2020-12-23 13:23

I have a bunch of strings, some of them have \' rec\'. I want to remove that only if those are the last 4 characters.

So in other words I have



        
相关标签:
11条回答
  • 2020-12-23 13:47

    If speed is not important, use regex:

    import re
    
    somestring='this is some string rec'
    
    somestring = re.sub(' rec$', '', somestring)
    
    0 讨论(0)
  • 2020-12-23 13:48
    def rchop(s, suffix):
        if suffix and s.endswith(suffix):
            return s[:-len(suffix)]
        return s
    
    somestring = 'this is some string rec'
    rchop(somestring, ' rec')  # returns 'this is some string'
    
    0 讨论(0)
  • 2020-12-23 13:48

    Since you have to get len(trailing) anyway (where trailing is the string you want to remove IF it's trailing), I'd recommend avoiding the slight duplication of work that .endswith would cause in this case. Of course, the proof of the code is in the timing, so, let's do some measurement (naming the functions after the respondents proposing them):

    import re
    
    astring = 'this is some string rec'
    trailing = ' rec'
    
    def andrew(astring=astring, trailing=trailing):
        regex = r'(.*)%s$' % re.escape(trailing)
        return re.sub(regex, r'\1', astring)
    
    def jack0(astring=astring, trailing=trailing):
        if astring.endswith(trailing):
            return astring[:-len(trailing)]
        return astring
    
    def jack1(astring=astring, trailing=trailing):
        regex = r'%s$' % re.escape(trailing)
        return re.sub(regex, '', astring)
    
    def alex(astring=astring, trailing=trailing):
        thelen = len(trailing)
        if astring[-thelen:] == trailing:
            return astring[:-thelen]
        return astring
    

    Say we've named this python file a.py and it's in the current directory; now, ...:

    $ python2.6 -mtimeit -s'import a' 'a.andrew()'
    100000 loops, best of 3: 19 usec per loop
    $ python2.6 -mtimeit -s'import a' 'a.jack0()'
    1000000 loops, best of 3: 0.564 usec per loop
    $ python2.6 -mtimeit -s'import a' 'a.jack1()'
    100000 loops, best of 3: 9.83 usec per loop
    $ python2.6 -mtimeit -s'import a' 'a.alex()'
    1000000 loops, best of 3: 0.479 usec per loop
    

    As you see, the RE-based solutions are "hopelessly outclassed" (as often happens when one "overkills" a problem -- possibly one of the reasons REs have such a bad rep in the Python community!-), though the suggestion in @Jack's comment is way better than @Andrew's original. The string-based solutions, as expected, shing, with my endswith-avoiding one having a miniscule advantage over @Jack's (being just 15% faster). So, both pure-string ideas are good (as well as both being concise and clear) -- I prefer my variant a little bit only because I am, by character, a frugal (some might say, stingy;-) person... "waste not, want not"!-)

    0 讨论(0)
  • 2020-12-23 13:54

    Taking inspiration from @David Foster's answer, I would do

    def _remove_suffix(text, suffix):
        if text is not None and suffix is not None:
            return text[:-len(suffix)] if text.endswith(suffix) else text
        else:
            return text
    

    Reference: Python string slicing

    0 讨论(0)
  • 2020-12-23 13:57

    Starting in Python 3.9, you can use removesuffix:

    'this is some string rec'.removesuffix(' rec')
    # 'this is some string'
    
    0 讨论(0)
提交回复
热议问题