判断字符串是否为标准IP地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
+(BOOL) isIPAdress :(NSString *)ip{
     
    NSArray *array = [ip componentsSeparatedByString:@"."];
    NSLog(@"number of array %ld",[array count]);
    for (NSString *sIP in array) {
        NSLog(@"%@",sIP);
    }
    BOOL flag = YES;
    if ([array count] == 4) {//判断是否为四段
        for (int i = 0; i<4; i++) {
            //判断是否由数字组成
            const char *str = [array[i] cStringUsingEncoding:NSUTF8StringEncoding];
            int j = 0;
            while (str[j] != ‘\0‘ ) {
                if (str[j] >= ‘0‘ && str[j] <= ‘9‘) {
                    j++;
                }else{
                    flag = NO;
                    break;
                }
            }
            //判断ip是否在0-255范围中
            if (flag) {
                NSInteger temp = [array[i] integerValue];
                if (temp < 0 || temp > 255) {
                    flag = NO;
                    break;
                }
            }
        }
    }else{
        flag = NO;
    }
    return flag;
}

判断字符串是否为标准IP地址

上一篇:《剑指Offer》面试题-重建二叉树


下一篇:excel函数