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 Jenkins <email@hidden>
- Date: Thu, 02 Apr 2015 00:17:00 -0400
Thank you very much. :-) I had been trying to figure out how to use NSCharacterSet, but I didn’t know the bit about converting to UTF-16 string first.
—
Charles
On April 1, 2015 at 9:52:47 PM, Charles Srstka (email@hidden) wrote:
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