Cocoa compare first character

Discussion in 'Mac Programming' started by wesg, Nov 29, 2008.

  1. wesg macrumors regular

    wesg

    Joined:
    Jan 2, 2008
    Location:
    Toronto, ON
    #1
    I'm just getting started in the world of Cocoa and Obj C and I'm stuck on a component. Aaron Hillegrass' book is helping me through, but my app keeps breaking. I learned C at university, and dabble with other languages here and there, so that helps, but the problem I'm having right now is comparing the first character of 2 strings. I think it has to do with the data types.

    What my goal for this piece of code, is to take a number (06 or 10, for example) and remove the leading zero if it has one. Maybe I'm missing a simple function, but here's what I have so far.

    Code:
    fileseason = @"06";
    firstchars = [NSString stringWithFormat:@"%d", [fileeason substringToIndex:1]];
    
    if (firstchars == 0)
    fileseason = [fileeason substringFromIndex:1];
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    deleted - Nevermind. At first I thought you needed to compare against a character.
     
  3. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    If you want to remove the leading zeros off an integer then

    Code:
    NSString *newString=[NSString stringWithFormat:@"%d",[oldString intValue]];
    will work...
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    eraserhead provided a solution, but I thought for future reference I'd cover what the OP had asked.
    -characterAtIndex provides a unichar which could be compared using == to another unichar to check against a particular value.

    -Lee
     

Share This Page