我有一个字符串string = 'some.value:so this-can be:any.thing, even some.value: too'
我想去掉左边的'some.value:'。
我的失败尝试:
>>> string.lstrip('some.value:')
' this-can be:any.thing, even some.value: too'
>>> string.replace('some.value:','')
'so this-can be:any.thing, even  too'
>>> string.split(':')[1]
'so this-can be'预期产出:so this-can be:any.thing, even some.value: too
我认为最接近我的答案是使用lstrip()。我怎么才能让lstrip()把整句话都删掉呢?
!最好不要使用任何库的!
注:有一个类似的question,但答案不适用于我。
发布于 2018-09-29 17:13:27
我们检查要剥离的字符串是否是开始,如果情况是这样,则剪切字符串:
def strip_from_start(strip, string):
    if string.startswith(strip):
        string = string[len(strip):]
    return string
print(strip_from_start('value:', 'value: xxx value: zzz'))
# xxx value: zzzhttps://stackoverflow.com/questions/52570610
复制相似问题