Skip to content

Instantly share code, notes, and snippets.

@brizandrew
Created August 5, 2016 19:44
Show Gist options
  • Save brizandrew/c4643d3d535f872f314c5161c68fc8e7 to your computer and use it in GitHub Desktop.
Save brizandrew/c4643d3d535f872f314c5161c68fc8e7 to your computer and use it in GitHub Desktop.
"""
@function indexOfAll
Utility function to find the index of every instance of a given substring in a string
@param {str} string: The string to search through
@param {str} sub: The substring to search for
@return {int[]}: A list of indeces everywhere that substring appears
"""
def indexOfAll(string, sub):
output = []
for i in range(0,len(string)):
try:
if string[i:i+len(sub)] == sub:
output.append(i)
except IndexError:
pass
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment