NSStrUtil.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // StrUtil.m
  3. // OrderFood
  4. //
  5. // Created by Berwin on 13-4-10.
  6. // Copyright (c) 2013年 Berwin. All rights reserved.
  7. //
  8. #import "NSStrUtil.h"
  9. #import <CommonCrypto/CommonDigest.h> // Need to import for CC_MD5 access
  10. @implementation NSStrUtil
  11. //+ (NSString *) getStoryNameByUrl:(NSString*) url
  12. //{
  13. // if ([NSStrUtil notEmptyOrNull:url]) {
  14. // NSRange range = [url rangeOfString:@"/" options:NSBackwardsSearch];
  15. // NSString *string = [url substringFromIndex:NSMaxRange(range)];
  16. // return string;
  17. // } else {
  18. // return nil;
  19. // }
  20. //}
  21. + (BOOL) isEmptyOrNull:(NSString*) string
  22. {
  23. return ![self notEmptyOrNull:string];
  24. }
  25. + (BOOL) notEmptyOrNull:(NSString*) string
  26. {
  27. if([string isKindOfClass:[NSNull class]])
  28. return NO;
  29. if ([string isKindOfClass:[NSNumber class]]) {
  30. if (string != nil) {
  31. return YES;
  32. }
  33. return NO;
  34. } else {
  35. string=[self trimString:string];
  36. if (string != nil && string.length > 0 && ![string isEqualToString:@"null"]&&![string isEqualToString:@"(null)"]&&![string isEqualToString:@" "]) {
  37. return YES;
  38. }
  39. return NO;
  40. }
  41. }
  42. + (BOOL)isMobileNumber:(NSString *)mobileNum
  43. {
  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[0125-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 * CM = @"^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 * CU = @"^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 * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
  66. /**
  67. 25 * 大陆地区固话及小灵通
  68. 26 * 区号:010,020,021,022,023,024,025,027,028,029
  69. 27 * 号码:七位或八位
  70. 28 */
  71. // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
  72. NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
  73. NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
  74. NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
  75. NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
  76. if (([regextestmobile evaluateWithObject:mobileNum] == YES)
  77. || ([regextestcm evaluateWithObject:mobileNum] == YES)
  78. || ([regextestct evaluateWithObject:mobileNum] == YES)
  79. || ([regextestcu evaluateWithObject:mobileNum] == YES))
  80. {
  81. return YES;
  82. }
  83. else
  84. {
  85. return NO;
  86. }
  87. }
  88. + (NSString*) makeNode:(NSString*) str{
  89. return [[NSString alloc] initWithFormat:@"<node>%@</node>", str];
  90. }
  91. + (NSString *)trimString:(NSString *) str {
  92. return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  93. }
  94. UIColor* colorFromHexRGB(NSString *inColorString){
  95. if ([NSStrUtil isEmptyOrNull:inColorString]) {
  96. return nil;
  97. }
  98. inColorString = [inColorString stringByReplacingOccurrencesOfString:@"#" withString:@""];
  99. if ([NSStrUtil isEmptyOrNull:inColorString]) {
  100. return nil;
  101. }
  102. UIColor *result = nil;
  103. unsigned int colorCode = 0;
  104. unsigned char redByte, greenByte, blueByte;
  105. if (nil != inColorString)
  106. {
  107. NSScanner *scanner = [NSScanner scannerWithString:inColorString];
  108. (void) [scanner scanHexInt:&colorCode]; // ignore error
  109. }
  110. redByte = (unsigned char) (colorCode >> 16);
  111. greenByte = (unsigned char) (colorCode >> 8);
  112. blueByte = (unsigned char) (colorCode); // masks off high bits
  113. result = [UIColor
  114. colorWithRed: (float)redByte / 0xff
  115. green: (float)greenByte/ 0xff
  116. blue: (float)blueByte / 0xff
  117. alpha:1.0];
  118. return result;
  119. }
  120. @end
  121. @implementation NSString (MyExtensions)
  122. - (NSString *) md5
  123. {
  124. const char *cStr = [self UTF8String];
  125. unsigned char result[16];
  126. CC_MD5( cStr, strlen(cStr), result ); // This is the md5 call
  127. return [NSString stringWithFormat:
  128. @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  129. result[0], result[1], result[2], result[3],
  130. result[4], result[5], result[6], result[7],
  131. result[8], result[9], result[10], result[11],
  132. result[12], result[13], result[14], result[15]
  133. ];
  134. }
  135. @end
  136. @implementation NSData (MyExtensions)
  137. - (NSString*)md5
  138. {
  139. unsigned char result[16];
  140. CC_MD5( self.bytes, self.length, result ); // This is the md5 call
  141. return [NSString stringWithFormat:
  142. @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  143. result[0], result[1], result[2], result[3],
  144. result[4], result[5], result[6], result[7],
  145. result[8], result[9], result[10], result[11],
  146. result[12], result[13], result[14], result[15]
  147. ];
  148. }
  149. @end