• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Swift: How to determine if a Character represents whitespace?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Swift: How to determine if a Character represents whitespace?
      • From: Charles Jenkins <email@hidden>
References: 
 >Swift: How to determine if a Character represents whitespace? (From: Charles Jenkins <email@hidden>)

  • Prev by Date: Swift: How to determine if a Character represents whitespace?
  • Next by Date: Re: Swift: How to determine if a Character represents whitespace?
  • Previous by thread: Swift: How to determine if a Character represents whitespace?
  • Next by thread: Re: Swift: How to determine if a Character represents whitespace?
  • Index(es):
    • Date
    • Thread