相当于str.l裂缝jython中的(char)<2.1

2024-05-16 09:37:37 发布

您现在位置:Python中文网/ 问答频道 /正文

考虑以下代码:

word='hello.world'
//matchedWord to contain everything right of "hello"
matchedWord=word.lstrip('hello') //now matchedWord='.world'

如何在Jython2.1中实现相同的功能str.l裂缝(char)不可用。有没有其他办法可以去掉一个单词剩下的所有字符?在


Tags: ofto代码功能righthelloworldnow
1条回答
网友
1楼 · 发布于 2024-05-16 09:37:37

如果您真的需要使用.lstrip(),您可以将其作为函数重新实现:

def lstrip(value, chars=None):
    if chars is None:
        chars=' \t\n'
    while value and value[0] in chars:
        value = value[1:]
    return value

但是您需要知道.lstrip()(和.rstrip()和{})从前面剥离字符集,而不是前缀。来自(cpython)文档:

Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:

>>> '   spacious   '.lstrip()
'spacious   '
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'

相关问题 更多 >