NSString ASCII filter

  • What is the simplest, reasonably efficient way to determine if an
    NSString contains a non-ASCII character?

    What I came up with was:

        NSCharacterSet *nonASCII = [[NSCharacterSet
    characterSetWithRange:NSMakeRange(0, 128)] invertedSet];
        NSRange nonASCIIRange = [testString
    rangeOfCharacterFromSet:nonASCII];
        if (nonASCIIRange.location != NSNotFound)
        {
            NSLog(@"got a non-ASCII character");
        }

    Thanks,
    James
  • This should be reasonably efficient.

    Aki

    On 2008/02/12, at 11:58, James Hober wrote:

    > What is the simplest, reasonably efficient way to determine if an
    > NSString contains a non-ASCII character?
    >
    > What I came up with was:
    >
    > NSCharacterSet *nonASCII = [[NSCharacterSet
    > characterSetWithRange:NSMakeRange(0, 128)] invertedSet];
    > NSRange nonASCIIRange = [testString
    > rangeOfCharacterFromSet:nonASCII];
    > if (nonASCIIRange.location != NSNotFound)
    > {
    > NSLog(@"got a non-ASCII character");
    > }
    >
    > Thanks,
    > James
  • On 2/12/08 11:58 AM, James Hober said:

    > What is the simplest, reasonably efficient way to determine if an
    > NSString contains a non-ASCII character

    How about:

    BOOL foo = [string canBeConvertedToEncoding:
      NSASCIIStringEncoding];

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
  • On Feb 12, 2008, at 12:04 PM, Sean McBride wrote:

    > On 2/12/08 11:58 AM, James Hober said:
    >
    >
    >> What is the simplest, reasonably efficient way to determine if an
    >> NSString contains a non-ASCII character
    >>
    >
    > How about:
    >
    > BOOL foo = [string canBeConvertedToEncoding:
    > NSASCIIStringEncoding];
    >

    Yes!  A one liner for simple task.  Thanks!

    James