Last active
October 18, 2016 19:55
-
-
Save scotteg/30ca59ca6365d3167743 to your computer and use it in GitHub Desktop.
A Swift String extension to check if a string is a palindrome
This file contains 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
extension String { | |
/** | |
Checks if string is a palindrome, ignoring spaces and capitalization. [Source on GitHub](http://bit.ly/SwiftStringPalindromeExtension) | |
- note: | |
[What's a palindrome?](https://en.wikipedia.org/wiki/Palindrome) | |
- returns: Bool | |
*/ | |
public func isPalindrome() -> Bool { | |
var s = String(self.characters.filter { $0 != Character(" ") }).lowercased() | |
return s == String(s.characters.reversed()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment