我想写一些在字符串中查找子字符串的代码。到目前为止,我有这样的:在Python中查找没有内置函数的字符串的子字符串
main = "dedicated"
sub = "cat"
count = 0
for i in range (0,len(main)):
match = True
if sub[0]==main[i]:
j=0
for j in range(0,len(sub)):
if sub[j]!=main[i+j]:
match = False
print "No substring"
break
else:
count=count+1
if match == True and count == len(sub):
print "Substring"
print "Position start:",i
- “奉献”和“猫”的作品
- “这是一个例子”和“榜样”返回
IndexError
- “一无所有”和“不同”的回报没有任何东西
任何人都可以帮助我/给我指针/改进代码,使其工作正确与上面的项目符号点?
你有问题吗? – jonrsharpe
您可以提供有关IndexError的更多详细信息吗? –
它返回“IndexError:字符串索引超出范围” – Sectah