function | annotations |
---|---|
split() | 剔除切口单元 并返回 断开的list(如果有 整段连续的 切口单元,则每个切口单元都剔除一次,连续的切口单元之间留下 "" ) |
strip() | 对字符串 末尾 剔除 并返回 完整的 字符串 |
string = 'Nanjing-is--the---capital----of-----Jiangshu------'
print string.split()
print string.split('-')
print string.split('--')
print string.strip()
print string.strip('-')
print string.strip('--')
打印结果:
['Nanjing-is--the---capital----of-----Jiangshu------']
['Nanjing', 'is', '', 'the', '', '', 'capital', '', '', '', 'of', '', '', '', '', 'Jiangshu', '', '', '', '', '', '']
['Nanjing-is', 'the', '-capital', '', 'of', '', '-Jiangshu', '', '', '']
Nanjing-is--the---capital----of-----Jiangshu------
Nanjing-is--the---capital----of-----Jiangshu
Nanjing-is--the---capital----of-----Jiangshu
leetcode: 28. Implement strStr():
class Solution():
def strStr(self, x, needle):
if not needle:
return 0
lst = x.split(needle)
if len(lst) == 1:
return -1
return len(lst[0])
if __name__ == "__main__":
assert Solution().strStr("a", "") == 0
assert Solution().strStr("abababcdab", "ababcdx") == -1