简单记录一下
def has_prefix(s, prefix):
return len(s) >= len(prefix) and s[:len(prefix)] == prefix
def has_suffix(s, suffix):
return len(s) >= len(suffix) and s[len(s)-len(suffix):] == suffix
def contains(s, substr):
for i, _ in enumerate(s):
if has_prefix(s[i:], substr):
return True
return False
print has_prefix("hello world", ‘hello‘)
print has_suffix("hello world", ‘world‘)
print contains("hello world", "lo")
原文地址:http://blog.51cto.com/cwtea/2084015