我想寫一些在字符串中查找子字符串的代碼。到目前爲止,我有這樣的:在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