以下是一个示例代码,通过使用字符串的in
操作符来判断一个字符串是否包含另一个字符串。
def filter_strings(strings, otherstring):
filtered_strings = []
for string in strings:
if "Otherstring" not in string:
filtered_strings.append(string)
return filtered_strings
strings = ["Somestring", "Anotherstring", "StringwithOtherstring"]
filtered = filter_strings(strings, "Otherstring")
print(filtered)
输出:
['Somestring', 'Anotherstring']
在上面的示例中,filter_strings
函数接受一个字符串列表和一个要排除的字符串作为参数。它遍历列表中的每个字符串,检查其中是否包含指定的“Otherstring”。如果没有包含,则将该字符串添加到过滤后的列表中。最后返回过滤后的列表。
上一篇:包括“espresso-contrib:3.1.0”后,任务“:app:processDebugAndroidTestResources”执行失败。
下一篇:包括“using System.Configuration”之后,仍然不能使用“ConfigurationManager.ConnectionStrings[...]”。