Skip to content

Instantly share code, notes, and snippets.

@airled
Created February 5, 2024 05:55
Show Gist options
  • Save airled/d53e33b08afa1b782e6b260b9dfeed98 to your computer and use it in GitHub Desktop.
Save airled/d53e33b08afa1b782e6b260b9dfeed98 to your computer and use it in GitHub Desktop.
Check substring in string
def subs(str1, str2)
return nil if str2.size > str1.size
str1.each_char.with_index do |char, index|
next if char != str2[0]
match = true
str2[1..].each_char.with_index do |_, str2_slice_index|
str2_index = str2_slice_index + 1
next if str2[str2_index] == str1[index + str2_index]
match = false
break
end
return index if match
end
nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment