Skip to content

Instantly share code, notes, and snippets.

@ianwcarlson
Last active July 29, 2018 19:10
Show Gist options
  • Save ianwcarlson/50c5320546a5937ecc3488958c8d0453 to your computer and use it in GitHub Desktop.
Save ianwcarlson/50c5320546a5937ecc3488958c8d0453 to your computer and use it in GitHub Desktop.
Find a string within a string without using a library method
# Find a string within a string and return the found index
# Found index will be -1 if string not found
def substring(strToSearch, strToFind):
totalLength = len(strToSearch)
findLength = len(strToFind)
for idx in range(0, totalLength):
if (strToSearch[idx:idx + findLength] == strToFind):
return idx
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment