Last active
July 29, 2018 19:10
-
-
Save ianwcarlson/50c5320546a5937ecc3488958c8d0453 to your computer and use it in GitHub Desktop.
Find a string within a string without using a library method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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