判断一个字符串是否再一个字符串数组里
以下是Python的实现:
def is_string_in_array(s, arr):
"""
判断一个字符串是否在一个字符串数组中
"""
return s in arr
使用示例:
s = "hello"
arr = ["world", "hello", "python"]
print(is_string_in_array(s, arr)) # 输出 True
如果需要忽略大小写,可以先将字符串数组中的所有字符串转换为小写或大写,再判断:
def is_string_in_array_case_insensitive(s, arr):
"""
判断一个字符串是否在一个字符串数组中(忽略大小写)
"""
arr = [x.lower() for x in arr] # 或者 [x.upper() for x in arr]
return s.lower() in arr # 或者 s.upper() in arr
使用示例:
s = "HeLLo"
arr = ["world", "HELLO", "python"]
print(is_string_in_array_case_insensitive(s, arr)) # 输出 True
原文地址: https://www.cveoy.top/t/topic/bRz8 著作权归作者所有。请勿转载和采集!