#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *num = [NSString stringWithFormat:@"420123199206262871"]; //创建字符串
NSMutableString *num2 = [[NSMutableString alloc] init]; //定义一个新的字符串来装num的前四位
NSString *num4 = @"4201";
NSString *num5 = @"1";
NSString *num6 = @"3";
for (int i = 0; i < 4; i++) //遍历
{
char ch = [num characterAtIndex:i]; //取出字符串前四位
NSMutableString *num3 = [NSMutableString stringWithFormat:@"%c",ch];
[num2 appendString:num3]; //将前四位装进num2
}
BOOL equal = [num2 isEqualToString:num4]; //比较与4201是不是一样的
if (equal == 1)
{
NSLog(@"此人是武汉市!");
}
else
NSLog(@"此人不是武汉市");
char last = [num characterAtIndex:17]; //取出身份证最后一位
NSString *string = [NSString stringWithFormat:@"%c",last];
BOOL yi = [string isEqualToString:num5];
BOOL san = [string isEqualToString:num6];
if(yi == 1 || san == 1)
{
NSLog(@"男性");
}
else
NSLog(@"不是男性");
}
return 0;
}
这个写的不错,,有更好的思路给我分享啊 |
|