123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- //
- // StrUtil.m
- // OrderFood
- //
- // Created by Berwin on 13-4-10.
- // Copyright (c) 2013年 Berwin. All rights reserved.
- //
- #import "NSStrUtil.h"
- #import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access
- @implementation NSStrUtil
- //+ (NSString *) getStoryNameByUrl:(NSString*) url
- //{
- // if ([NSStrUtil notEmptyOrNull:url]) {
- // NSRange range = [url rangeOfString:@"/" options:NSBackwardsSearch];
- // NSString *string = [url substringFromIndex:NSMaxRange(range)];
- // return string;
- // } else {
- // return nil;
- // }
- //}
- + (BOOL) isEmptyOrNull:(NSString*) string
- {
- return ![self notEmptyOrNull:string];
-
- }
- + (BOOL) notEmptyOrNull:(NSString*) string
- {
- if([string isKindOfClass:[NSNull class]])
- return NO;
- if ([string isKindOfClass:[NSNumber class]]) {
- if (string != nil) {
- return YES;
- }
- return NO;
- } else {
- string=[self trimString:string];
- if (string != nil && string.length > 0 && ![string isEqualToString:@"null"]&&![string isEqualToString:@"(null)"]&&![string isEqualToString:@" "]) {
- return YES;
- }
- return NO;
- }
- }
- + (BOOL)isMobileNumber:(NSString *)mobileNum
- {
- /**
- * 手机号码
- * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
- * 联通:130,131,132,152,155,156,185,186
- * 电信:133,1349,153,180,189
- */
- NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[0125-9])\\d{8}$";
- /**
- 10 * 中国移动:China Mobile
- 11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
- 12 */
- NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
- /**
- 15 * 中国联通:China Unicom
- 16 * 130,131,132,152,155,156,185,186
- 17 */
- NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
- /**
- 20 * 中国电信:China Telecom
- 21 * 133,1349,153,180,189
- 22 */
- NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
- /**
- 25 * 大陆地区固话及小灵通
- 26 * 区号:010,020,021,022,023,024,025,027,028,029
- 27 * 号码:七位或八位
- 28 */
- // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
-
- NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
- NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
- NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
- NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
-
- if (([regextestmobile evaluateWithObject:mobileNum] == YES)
- || ([regextestcm evaluateWithObject:mobileNum] == YES)
- || ([regextestct evaluateWithObject:mobileNum] == YES)
- || ([regextestcu evaluateWithObject:mobileNum] == YES))
- {
- return YES;
- }
- else
- {
- return NO;
- }
- }
- + (NSString*) makeNode:(NSString*) str{
- return [[NSString alloc] initWithFormat:@"<node>%@</node>", str];
- }
- + (NSString *)trimString:(NSString *) str {
- return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- }
- UIColor* colorFromHexRGB(NSString *inColorString){
-
- if ([NSStrUtil isEmptyOrNull:inColorString]) {
- return nil;
- }
-
- inColorString = [inColorString stringByReplacingOccurrencesOfString:@"#" withString:@""];
-
- if ([NSStrUtil isEmptyOrNull:inColorString]) {
- return nil;
- }
-
- UIColor *result = nil;
- unsigned int colorCode = 0;
- unsigned char redByte, greenByte, blueByte;
-
- if (nil != inColorString)
- {
- NSScanner *scanner = [NSScanner scannerWithString:inColorString];
- (void) [scanner scanHexInt:&colorCode]; // ignore error
- }
- redByte = (unsigned char) (colorCode >> 16);
- greenByte = (unsigned char) (colorCode >> 8);
- blueByte = (unsigned char) (colorCode); // masks off high bits
- result = [UIColor
- colorWithRed: (float)redByte / 0xff
- green: (float)greenByte/ 0xff
- blue: (float)blueByte / 0xff
- alpha:1.0];
- return result;
- }
- @end
- @implementation NSString (MyExtensions)
- - (NSString *) md5
- {
- const char *cStr = [self UTF8String];
- unsigned char result[16];
- CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call
- return [NSString stringWithFormat:
- @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
- result[0], result[1], result[2], result[3],
- result[4], result[5], result[6], result[7],
- result[8], result[9], result[10], result[11],
- result[12], result[13], result[14], result[15]
- ];
- }
- @end
- @implementation NSData (MyExtensions)
- - (NSString*)md5
- {
- unsigned char result[16];
- CC_MD5( self.bytes, self.length, result ); // This is the md5 call
- return [NSString stringWithFormat:
- @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
- result[0], result[1], result[2], result[3],
- result[4], result[5], result[6], result[7],
- result[8], result[9], result[10], result[11],
- result[12], result[13], result[14], result[15]
- ];
- }
- @end
|