Re: Swift: How to determine if a Character represents whitespace?
Re: Swift: How to determine if a Character represents whitespace?
- Subject: Re: Swift: How to determine if a Character represents whitespace?
- From: Charles Srstka <email@hidden>
- Date: Wed, 01 Apr 2015 20:52:34 -0500
On Apr 1, 2015, at 8:14 PM, Charles Jenkins <email@hidden> wrote:
>
> Given this code:
>
> let someCharacter = str[str.endIndex.predecessor()]
>
> How can I determine if someCharacter is whitespace?
import Foundation
func isChar(char: Character, inSet set: NSCharacterSet) -> Bool {
// this function is from an answer on StackOverflow:
// http://stackoverflow.com/questions/27697508/nscharacterset-characterismember-with-swifts-character-type
var found = true
for ch in String(char).utf16 {
if !set.characterIsMember(ch) { found = false }
}
return found
}
let str = "foo "
let chr = str[str.endIndex.predecessor()]
let isWhitespace = isChar(chr, inSet: NSCharacterSet.whitespaceAndNewlineCharacterSet()) // true
Charles
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden