ios常用的读取文件路径操作以及正则表达式

ios常用的读取文件路径操作以及正则表达式


//

//  NSString+Common.h

//  CatagoryDemo

//

//  Created by 张洋洋 on 14-11-13.

//  Copyright (c) 2014 张洋洋. All rights reserved.

//

  1. #import <Foundation/Foundation.h>  
  2.   
  3. /*! 
  4.  * @brief NSString 扩展 
  5.  */  
  6. @interface NSString (Common)  
  7.   
  8. // 获取Documents路径  
  9. + (NSString *)documentPath;  
  10.   
  11. // 获取缓存路径  
  12. + (NSString *)cachePath;  
  13.   
  14. + (NSString *)imageCachePath;  
  15.   
  16. // 本地购物车路径  
  17. + (NSString *)localShoppingCartPath;  
  18.   
  19. //! 是否是合法邮箱  
  20. - (BOOL)isValidEmail;  
  21. //! 是否是合法号码  
  22. - (BOOL)isValidPhoneNumber;  
  23. //! 是否是合法的18位身份证号码    
  24. - (BOOL)isValidPersonID;  
  25. /** 
  26.  * 功能:判断是否在地区码内 
  27.  * 参数:地区码 
  28.  */  
  29. - (BOOL)areaCode:(NSString *)code;  
  30.   
  31. //! 根据文件名返回路径  
  32. + (NSString *)pathWithFileName:(NSString *)fileName;  
  33. + (NSString *)pathWithFileName:(NSString *)fileName ofType:(NSString *)type;  
  34.   
  35. // 根据秒数返回日期  
  36. + (NSString *)dateWithSeconds:(NSUInteger)seconds;  
  37.   
  38. @end

  39.   

//

//  NSString+Common.m

//  CatagoryDemo

//

//  Created by 张洋洋 on 14-11-13.

//  Copyright (c) 2014 张洋洋. All rights reserved.

//

  1. #import "NSString+Common.h"  
  2.   
  3. @implementation NSString (Common)  
  4.   
  5. + (NSString *)documentPath {  
  6.     return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];  
  7. }  
  8.   
  9. #pragma mark - 获取缓存路径  
  10. + (NSString *)cachePath {  
  11.     return [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];  
  12. }  
  13.   
  14. + (NSString *)imageCachePath {  
  15.     NSString *path = [[self cachePath] stringByAppendingPathComponent:@"Images"];  
  16.     BOOL isDir = NO;  
  17.     BOOL isDirExist = [[NSFileManager defaultManager] fileExistsAtPath:path  
  18.                                                            isDirectory:&isDir];  
  19.     if (!isDir && !isDirExist) {  
  20.        BOOL isSuccess = [[NSFileManager defaultManager] createDirectoryAtPath:path  
  21.                                   withIntermediateDirectories:YES  
  22.                                                    attributes:nil error:nil];  
  23.         if (isSuccess) {  
  24.             NSLog(@"success");  
  25.         }  
  26.     }  
  27.       
  28.     return path;  
  29. }  
  30.   
  31. + (NSString *)localShoppingCartPath {  
  32.     return [[self cachePath] stringByAppendingPathComponent:@"/cart.plist"];  
  33. }  
  34.   
  35. #pragma mark - 验证邮箱格式  
  36. - (BOOL)isValidEmail {  
  37.     NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";  
  38.     NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];  
  39.     return [emailTest evaluateWithObject:self];  
  40. }  
  41.   
  42. #pragma mark - 验证手机号码格式  
  43. - (BOOL)isValidPhoneNumber {  
  44.     /** 
  45.      * 手机号码 
  46.      * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 
  47.      * 联通:130,131,132,152,155,156,185,186 
  48.      * 电信:133,1349,153,180,189 
  49.      */  
  50.     NSString *mobile = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";  
  51.     /** 
  52.      10         * 中国移动:China Mobile 
  53.      11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 
  54.      12         */  
  55.     NSString *chinaMobile = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";  
  56.     /** 
  57.      15         * 中国联通:China Unicom 
  58.      16         * 130,131,132,152,155,156,185,186 
  59.      17         */  
  60.     NSString * chinaUnicom = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";  
  61.     /** 
  62.      20         * 中国电信:China Telecom 
  63.      21         * 133,1349,153,180,189 
  64.      22         */  
  65.     NSString * chinaTelecom = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";  
  66.       
  67.     NSPredicate *mobilePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", mobile];  
  68.     NSPredicate *cmPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", chinaMobile];  
  69.     NSPredicate *cuPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", chinaUnicom];  
  70.     NSPredicate *ctPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", chinaTelecom];  
  71.     if ([mobilePredicate evaluateWithObject:self]  
  72.         || [cmPredicate evaluateWithObject:self]  
  73.         || [cuPredicate evaluateWithObject:self]  
  74.         || [ctPredicate evaluateWithObject:self]) {  
  75.         return YES;  
  76.     }  
  77.       
  78.     return NO;  
  79. }  
  80.   
  81. /** 
  82.  * 功能:验证身份证是否合法 
  83.  * 参数:输入的身份证号 
  84.  */  
  85. - (BOOL)isValidPersonID {  
  86.     // 判断位数  
  87.     if (self.length != 15 && self.length != 18) {  
  88.         return NO;  
  89.     }  
  90.     NSString *carid = self;  
  91.     long lSumQT = 0;  
  92.     // 加权因子  
  93.     int R[] ={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };  
  94.     // 校验码  
  95.     unsigned char sChecker[11]={'1','0','X''9''8''7''6''5''4''3''2'};  
  96.   
  97.     // 15位身份证号转换成18  
  98.     NSMutableString *mString = [NSMutableString stringWithString:self];  
  99.     if (self.length == 15) {  
  100.         [mString insertString:@"19" atIndex:6];  
  101.         long p = 0;  
  102.         const charchar *pid = [mString UTF8String];  
  103.           
  104.         for (int i = 0; i<= 16; i++) {  
  105.             p += (pid[i] - 48) * R[i];  
  106.         }  
  107.           
  108.         int o = p % 11;  
  109.         NSString *string_content = [NSString stringWithFormat:@"%c", sChecker[o]];  
  110.         [mString insertString:string_content atIndex:[mString length]];  
  111.         carid = mString;  
  112.     }  
  113.       
  114.     // 判断地区码  
  115.     NSString * sProvince = [carid substringToIndex:2];  
  116.     if (![self areaCode:sProvince]) {  
  117.         return NO;  
  118.     }  
  119.       
  120.     // 判断年月日是否有效  
  121.     // 年份  
  122.     int strYear = [[self substringWithString:carid begin:6 end:4] intValue];  
  123.     // 月份  
  124.     int strMonth = [[self substringWithString:carid begin:10 end:2] intValue];  
  125.     //   
  126.     int strDay = [[self substringWithString:carid begin:12 end:2] intValue];  
  127.       
  128.     NSTimeZone *localZone = [NSTimeZone localTimeZone];  
  129.     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];  
  130.     [dateFormatter setDateStyle:NSDateFormatterMediumStyle];  
  131.     [dateFormatter setTimeStyle:NSDateFormatterNoStyle];  
  132.     [dateFormatter setTimeZone:localZone];  
  133.     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];  
  134.     NSDate *date=[dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d 12:01:01",  
  135.                                                 strYear, strMonth, strDay]];  
  136.     if (date == nil) {  
  137.         return NO;  
  138.     }  
  139.       
  140.     const charchar *PaperId  = [carid UTF8String];  
  141.     // 检验长度  
  142.     if(18 != strlen(PaperId)) return NO;  
  143.     // 校验数字  
  144.     for (int i = 0; i < 18; i++) {  
  145.         if ( !isdigit(PaperId[i]) && !(('X' == PaperId[i] || 'x' == PaperId[i]) && 17 == i) ) {  
  146.             return NO;  
  147.         }  
  148.     }  
  149.       
  150.     // 验证最末的校验码  
  151.     for (int i=0; i<=16; i++) {  
  152.         lSumQT += (PaperId[i]-48) * R[i];  
  153.     }  
  154.       
  155.     if (sChecker[lSumQT%11] != PaperId[17] ) {  
  156.         return NO;  
  157.     }  
  158.     return YES;  
  159. }  
  160.   
  161. /** 
  162.  * 功能:判断是否在地区码内 
  163.  * 参数:地区码 
  164.  */  
  165. - (BOOL)areaCode:(NSString *)code {  
  166.     NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];  
  167.     [dic setObject:@"北京" forKey:@"11"];  
  168.     [dic setObject:@"天津" forKey:@"12"];  
  169.     [dic setObject:@"河北" forKey:@"13"];  
  170.     [dic setObject:@"山西" forKey:@"14"];  
  171.     [dic setObject:@"内蒙古" forKey:@"15"];  
  172.     [dic setObject:@"辽宁" forKey:@"21"];  
  173.     [dic setObject:@"吉林" forKey:@"22"];  
  174.     [dic setObject:@"黑龙江" forKey:@"23"];  
  175.     [dic setObject:@"上海" forKey:@"31"];  
  176.     [dic setObject:@"江苏" forKey:@"32"];  
  177.     [dic setObject:@"浙江" forKey:@"33"];  
  178.     [dic setObject:@"安徽" forKey:@"34"];  
  179.     [dic setObject:@"福建" forKey:@"35"];  
  180.     [dic setObject:@"江西" forKey:@"36"];  
  181.     [dic setObject:@"山东" forKey:@"37"];  
  182.     [dic setObject:@"河南" forKey:@"41"];  
  183.     [dic setObject:@"湖北" forKey:@"42"];  
  184.     [dic setObject:@"湖南" forKey:@"43"];  
  185.     [dic setObject:@"广东" forKey:@"44"];  
  186.     [dic setObject:@"广西" forKey:@"45"];  
  187.     [dic setObject:@"海南" forKey:@"46"];  
  188.     [dic setObject:@"重庆" forKey:@"50"];  
  189.     [dic setObject:@"四川" forKey:@"51"];  
  190.     [dic setObject:@"贵州" forKey:@"52"];  
  191.     [dic setObject:@"云南" forKey:@"53"];  
  192.     [dic setObject:@"西藏" forKey:@"54"];  
  193.     [dic setObject:@"陕西" forKey:@"61"];  
  194.     [dic setObject:@"甘肃" forKey:@"62"];  
  195.     [dic setObject:@"青海" forKey:@"63"];  
  196.     [dic setObject:@"宁夏" forKey:@"64"];  
  197.     [dic setObject:@"新疆" forKey:@"65"];  
  198.     [dic setObject:@"台湾" forKey:@"71"];  
  199.     [dic setObject:@"香港" forKey:@"81"];  
  200.     [dic setObject:@"澳门" forKey:@"82"];  
  201.     [dic setObject:@"国外" forKey:@"91"];  
  202.       
  203.     if ([dic objectForKey:code] == nil) {  
  204.         return NO;  
  205.     }  
  206.     return YES;  
  207. }  
  208.   
  209. #pragma mark - 根据文件名返回路径  
  210. + (NSString *)pathWithFileName:(NSString *)fileName {  
  211.     return [self pathWithFileName:fileName ofType:nil];  
  212. }  
  213.   
  214. + (NSString *)pathWithFileName:(NSString *)fileName ofType:(NSString *)type {  
  215.     return [[NSBundle mainBundle] pathForResource:fileName ofType:type];  
  216. }  
  217.   
  218. /** 
  219.  * 功能:获取指定范围的字符串 
  220.  * 参数:字符串的开始小标 
  221.  * 参数:字符串的结束下标 
  222.  */  
  223. - (NSString *)substringWithString:(NSString *)str begin:(NSInteger)begin end:(NSInteger )end {  
  224.     return [str substringWithRange:NSMakeRange(begin, end)];  
  225. }  
  226.   
  227. + (NSString *)dateWithSeconds:(NSUInteger)seconds {  
  228.     NSDate   *date   = [NSDate dateWithTimeIntervalSince1970:seconds];  
  229.     NSString *str    = [NSString stringWithFormat:@"%@", date];  
  230.     NSArray  *array  = [str componentsSeparatedByString:@" "];  
  231.     NSString *result = [array objectAtIndex:0];  
  232.     if (array.count == 3) {  
  233.         result = [NSString stringWithFormat:@"%@ %@", result, [array objectAtIndex:1]];  
  234.     }  
  235.     return result;  
  236. }  
  237.   
  238. @end  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值