在字符串中查找子字符串并在 Python 中返回子字符串的索引
def find_str(s, char):
index = 0
if char in s:
c = char[0]
for ch in s:
if ch == c:
if s[index:index + len(char)] == char:
return index
index += 1
return -1
print(find_str("India Canada Japan", "Canada"))
print(find_str("India Canada Japan", "cana"))
print(find_str("India Canada Japan", "Uae"))
Output:
6
-1
-1