开发者

charaterAtIndex Issues

开发者 https://www.devze.com 2023-02-07 06:09 出处:网络
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 corre

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

精彩评论

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