1

我有一个像这样的字符串 NSString *mobile=@"09894598945"。如何检查上面字符串的第一个字符为零?

4

2 回答 2

1

As suggested in comment hasPrefix is best fit here. But you can also use substringWithRange function as well. It is enough flexible to check nth character of the string as well:

if([[mobile substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"5"])
{
   // started with 5
}

EDIT

As discussed in comment with Nikolai Ruhe, function substringWithRange will not work for surrogate pairs as mentioned in comments. This is the limitation of this function.

Use hasPrefix instead.

于 2013-09-23T08:28:48.743 回答
0
NSString *mobile=@"09894598945";

    if ([mobile characterAtIndex:0] == '0')
    {
        NSLog(@"Yes it contain 0");
    }

或者

if ([mobile hasPrefix:@"0"])
    {
        NSLog(@"Yes it contain 0");
    }
于 2013-09-23T08:47:40.327 回答