Skip to content

Instantly share code, notes, and snippets.

@swift-student
Created March 11, 2020 15:20
Show Gist options
  • Save swift-student/8d62c92fc61bdb5c498cfcba41dfccf5 to your computer and use it in GitHub Desktop.
Save swift-student/8d62c92fc61bdb5c498cfcba41dfccf5 to your computer and use it in GitHub Desktop.

To start, the challenge specifies I need to make an extension on string, so I will set that up. Then, since we are ignoring case, I will go ahead and create lowercased versions of both the string passed in and self. I will then see about using regular expressions to determine if self contains the string passed in.

Ok, so turns out, the .rangeOf function has an options parameter which could allow us to use a regular expression, or simply specify .caseInsensitive. So this challenge is solved in one line. Kinda lame. Maybe come up with a harder challenge that forces us to use a regular expression...

extension String {
    func anotherContains(_ string: String) -> Bool {
        self.range(of: string, options: .caseInsensitive) != nil
    }
}

print("Where is WaLdO".anotherContains("WALDO")) // true
print("Where is WaLdO".anotherContains("Shawn")) // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment