PDA

View Full Version : Cocoa compare first character




wesg
Nov 29, 2008, 03:52 PM
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.

fileseason = @"06";
firstchars = [NSString stringWithFormat:@"%d", [fileeason substringToIndex:1]];

if (firstchars == 0)
fileseason = [fileeason substringFromIndex:1];



toddburch
Nov 29, 2008, 04:43 PM
deleted - Nevermind. At first I thought you needed to compare against a character.

Eraserhead
Nov 29, 2008, 05:17 PM
If you want to remove the leading zeros off an integer then

NSString *newString=[NSString stringWithFormat:@"%d",[oldString intValue]];

will work...

lee1210
Nov 29, 2008, 07:04 PM
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