开发者

charaterAtIndex Issues

What's wrong with this code. I am trying to remove the leading zero from a String that contains the date. The con开发者_运维问答dition below seems to never be true. Is this use of characterAtIndex correct? Is there an alternative?

    // new_date = 01/26/11
    NSString *displayDate = [[NSString alloc] init];
    NSRange date_leading_zero = NSMakeRange (0,1);
    if([new_date characterAtIndex:0] == @"0")
 {
  displayDate = [new_date stringByReplacingCharactersInRange:date_leading_zero withString:@""]; 
 }


if([new_date characterAtIndex:0] == @"0")

should be

if([new_date characterAtIndex:0] == '0')

-characterAtIndex: returns a unichar (a typedef for a 16-bit unsigned int) and not an NSString.


[new_date characterAtIndex:0] returns an unsigned short, but @"0" is an NSString, so comparing them with == will essentially never succeed. Use

if( [new_date characterAtIndex:0] == '0' )

instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜