以下是一个示例代码,用于比较两个字符串数组以匹配一个单词:
def find_matching_word(arr1, arr2, word):
for str1 in arr1:
for str2 in arr2:
if str1 + str2 == word:
return True
return False
# 测试示例
arr1 = ["hello", "world"]
arr2 = ["abc", "def"]
word = "helloworld"
result = find_matching_word(arr1, arr2, word)
print(result) # 输出:True
在上面的示例中,我们定义了一个名为find_matching_word的函数,它接受三个参数:arr1和arr2是两个字符串数组,word是要匹配的单词。
函数使用嵌套的循环遍历arr1和arr2中的所有字符串组合,然后将它们拼接在一起,与word进行比较。如果找到匹配的字符串组合,函数将返回True,否则返回False。
在测试示例中,我们声明了arr1和arr2两个字符串数组,以及要匹配的单词word。然后调用find_matching_word函数,并将结果打印出来。在这个示例中,arr1中的"hello"和arr2中的"world"可以组合成"helloworld",因此结果为True。
下一篇:比较两个字符串相同位置的字符