def find_index(str1, str2):
for i in range(min(len(str1), len(str2))):
if str1[i] == str2[i]:
return i
return -1
# 测试代码
a = "hello"
b = "world"
print(find_index(a, b)) # 输出:0
c = "python"
d = "java"
print(find_index(c, d)) # 输出:2
该函数接受两个字符串作为输入,返回它们第一个匹配的字符的索引,如果没有匹配项则返回-1。它会迭代两个字符串,只要找到第一个匹配项就会返回其索引。如果没有匹配项,则返回-1作为标志。在上面的代码示例中,我们使用了两个不同的字符串对函数进行了测试并打印了输出结果。