Skip to content

Instantly share code, notes, and snippets.

@scotteg
Last active October 18, 2016 19:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scotteg/30ca59ca6365d3167743 to your computer and use it in GitHub Desktop.
Save scotteg/30ca59ca6365d3167743 to your computer and use it in GitHub Desktop.
A Swift String extension to check if a string is a palindrome
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