How to check the capital letter?

  • HI!
    Cocoa, Obj-C.
    How to check the capital letter?

          ________________________________________________________
    Ð’Ñ‹ уже Ñ? Yahoo!?
    ИÑ?пытайте обновленную и улучшенную. Yahoo! Почту! http://ru.mail.yahoo.com
  • On Aug 11, 2008, at 8:52 AM, Macarov Anatoli wrote:

    > HI!
    > Cocoa, Obj-C.

    Hi,

    You'll want to look into NSScanner and NSCharacterSet. The String
    Programming Guide should provide all you need...

    http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/index.htm
    l


    hope that helps,

    Jaime Magiera

    Sensory Research
    http://www.sensoryresearch.net
  • On 11 Aug 2008, at 13:52, Macarov Anatoli wrote:

    > HI!
    > Cocoa, Obj-C.
    > How to check the capital letter?

    You really need to explain what it is you actually want to do.

  • On 11/08/2008, at 10:52 PM, Macarov Anatoli wrote:

    > HI!
    > Cocoa, Obj-C.
    > How to check the capital letter?

    Hi, I don't remember if there is a Cocoa solution, but of course you
    can use plain C:

    NSString *str = @"Aa";
    char first = [str characterAtIndex:0];
    char second = [str characterAtIndex:1];
    NSLog(@"%c is %@.", first, isupper(first) ? @"uppercase" :
    @"lowercase");
    NSLog(@"%c is %@.", second, isupper(second) ? @"uppercase" :
    @"lowercase");

    Result:

    A is uppercase.
    a is lowercase.
  • Le 11 août 08 à 15:29, Ron Fleckner a écrit :

    >
    > On 11/08/2008, at 10:52 PM, Macarov Anatoli wrote:
    >
    >> HI!
    >> Cocoa, Obj-C.
    >> How to check the capital letter?
    >
    > Hi, I don't remember if there is a Cocoa solution, but of course you
    > can use plain C:
    >
    > NSString *str = @"Aa";
    > char first = [str characterAtIndex:0];
    > char second = [str characterAtIndex:1];
    > NSLog(@"%c is %@.", first, isupper(first) ? @"uppercase" :
    > @"lowercase");
    > NSLog(@"%c is %@.", second, isupper(second) ? @"uppercase" :
    > @"lowercase");
    >
    > Result:
    >
    > A is uppercase.
    > a is lowercase.

    Wrong. characterAtIndex: return an unichar not a char, which is AFAK
    an UTF16 character.
  • On 11/08/2008, at 11:29 PM, Ron Fleckner wrote:

    >
    > On 11/08/2008, at 10:52 PM, Macarov Anatoli wrote:
    >
    >> HI!
    >> Cocoa, Obj-C.
    >> How to check the capital letter?
    >
    > Hi, I don't remember if there is a Cocoa solution, but of course
    > you can use plain C:
    >
    > NSString *str = @"Aa";
    > char first = [str characterAtIndex:0];
    > char second = [str characterAtIndex:1];
    > NSLog(@"%c is %@.", first, isupper(first) ? @"uppercase" :
    > @"lowercase");
    > NSLog(@"%c is %@.", second, isupper(second) ? @"uppercase" :
    > @"lowercase");
    >
    > Result:
    >
    > A is uppercase.
    > a is lowercase.
    >

    Forgot to mention: this isn't plain C, but a function from the C
    library.  No need to explicitly link, it just works.

    Ron
  • On 11/08/2008, at 11:35 PM, Jean-Daniel Dupas wrote:

    >
    > Le 11 août 08 à 15:29, Ron Fleckner a écrit :
    >
    >>
    >> Hi, I don't remember if there is a Cocoa solution, but of course
    >> you can use plain C:
    >>
    >> NSString *str = @"Aa";
    >> char first = [str characterAtIndex:0];
    >> char second = [str characterAtIndex:1];
    >> NSLog(@"%c is %@.", first, isupper(first) ? @"uppercase" :
    >> @"lowercase");
    >> NSLog(@"%c is %@.", second, isupper(second) ? @"uppercase" :
    >> @"lowercase");
    >>
    >> Result:
    >>
    >> A is uppercase.
    >> a is lowercase.
    >
    > Wrong. characterAtIndex: return an unichar not a char, which is
    > AFAK an UTF16 character.

    Yes, I saw that.  But it still works.  No compiler warning either.
    Would it be a problem?

    Ron
  • On 11.08.2008, at 15:29, Ron Fleckner wrote:
    > NSString *str = @"Aa";
    > char first = [str characterAtIndex:0];
    > char second = [str characterAtIndex:1];
    > NSLog(@"%c is %@.", first, isupper(first) ? @"uppercase" :
    > @"lowercase");
    > NSLog(@"%c is %@.", second, isupper(second) ? @"uppercase" :
    > @"lowercase");
    >
    > Result:
    >
    > A is uppercase.
    > a is lowercase.

      Bad Idea (tm):

    You're discarding the high byte of the unichar that characterAtIndex:
    returns by casting it to a regular char, so isupper() might get to see
    a completely different character than the user sees.

    Second, Unicode can have decomposed character sequences, so the glyph
    at that particular index may actually be several characters long, and
    you're only analyzing the first one.

    NSCharacterSet is probably your best bet. You could check whether
    whatever character you're looking at is a member of the
    uppercaseLetterCharacterSet.

    Cheers,
    -- Uli Kusterer
    "The Witnesses of TeachText are everywhere..."
    http://www.zathras.de
  • On 11/08/2008, at 11:54 PM, Uli Kusterer wrote:

    > On 11.08.2008, at 15:29, Ron Fleckner wrote:
    >> NSString *str = @"Aa";
    >> char first = [str characterAtIndex:0];
    >> char second = [str characterAtIndex:1];
    >> NSLog(@"%c is %@.", first, isupper(first) ? @"uppercase" :
    >> @"lowercase");
    >> NSLog(@"%c is %@.", second, isupper(second) ? @"uppercase" :
    >> @"lowercase");
    >>
    >> Result:
    >>
    >> A is uppercase.
    >> a is lowercase.
    >
    > Bad Idea (tm):
    >
    > You're discarding the high byte of the unichar that
    > characterAtIndex: returns by casting it to a regular char, so
    > isupper() might get to see a completely different character than
    > the user sees.
    >
    > Second, Unicode can have decomposed character sequences, so the
    > glyph at that particular index may actually be several characters
    > long, and you're only analyzing the first one.
    >
    > NSCharacterSet is probably your best bet. You could check whether
    > whatever character you're looking at is a member of the
    > uppercaseLetterCharacterSet.
    >
    > Cheers,
    > -- Uli Kusterer

    OK, thanks Uli and Jean-Daniel.  I see why it's not such a good idea.

    Ron
  • > How to check the capital letter?

    If you don't want to mess around with NSCharacterSet and NSScanner,
    you could also do this:

    NSString *string = @"abcABC";
    NSUInteger index = 0; // The character at this index will be checked
    NSString *character = [string substringWithRange:NSMakeRange(index, 1)];
    NSLog(@"is uppercase: %i", [character isEqualToString:[character
    uppercaseString]]);