NSString+Utils.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // NSString+Utils.m
  3. // MIT_Endorsement
  4. //
  5. // Created by 翟玉磊 on 2018/1/30.
  6. // Copyright © 2018年 翟玉磊. All rights reserved.
  7. //
  8. #import "NSString+Utils.h"
  9. @implementation NSString (Utils)
  10. /**
  11. * 截取URL中的参数
  12. *
  13. * @return NSMutableDictionary parameters
  14. */
  15. - (NSMutableDictionary *)getURLParameters {
  16. // 查找参数
  17. NSRange range = [self rangeOfString:@"?"];
  18. if (range.location == NSNotFound) {
  19. return nil;
  20. }
  21. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  22. // 截取参数
  23. NSString *parametersString = [self substringFromIndex:range.location + 1];
  24. // 判断参数是单个参数还是多个参数
  25. if ([parametersString containsString:@"&"]) {
  26. // 多个参数,分割参数
  27. NSArray *urlComponents = [parametersString componentsSeparatedByString:@"&"];
  28. for (NSString *keyValuePair in urlComponents) {
  29. // 生成Key/Value
  30. NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="];
  31. NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding];
  32. NSString *value = [pairComponents.lastObject stringByRemovingPercentEncoding];
  33. // Key不能为nil
  34. if (key == nil || value == nil) {
  35. continue;
  36. }
  37. id existValue = [params valueForKey:key];
  38. if (existValue != nil) {
  39. // 已存在的值,生成数组
  40. if ([existValue isKindOfClass:[NSArray class]]) {
  41. // 已存在的值生成数组
  42. NSMutableArray *items = [NSMutableArray arrayWithArray:existValue];
  43. [items addObject:value];
  44. [params setValue:items forKey:key];
  45. } else {
  46. // 非数组
  47. [params setValue:@[existValue, value] forKey:key];
  48. }
  49. } else {
  50. // 设置值
  51. [params setValue:value forKey:key];
  52. }
  53. }
  54. } else {
  55. // 单个参数
  56. // 生成Key/Value
  57. NSArray *pairComponents = [parametersString componentsSeparatedByString:@"="];
  58. // 只有一个参数,没有值
  59. if (pairComponents.count == 1) {
  60. return nil;
  61. }
  62. // 分隔值
  63. NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding];
  64. NSString *value = [pairComponents.lastObject stringByRemovingPercentEncoding];
  65. // Key不能为nil
  66. if (key == nil || value == nil) {
  67. return nil;
  68. }
  69. // 设置值
  70. [params setValue:value forKey:key];
  71. }
  72. return params;
  73. }
  74. /**
  75. 获取缓存路径
  76. @return 将当前字符串拼接到cache目录后面
  77. */
  78. - (NSString *)cacheDic
  79. {
  80. NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  81. return [path stringByAppendingPathComponent:self.lastPathComponent];
  82. }
  83. /**
  84. 获取document路径
  85. @return 将当前字符串拼接到document目录后面
  86. */
  87. - (NSString *)docDic
  88. {
  89. NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  90. return [path stringByAppendingPathComponent:self.lastPathComponent];
  91. }
  92. /**
  93. 获取tmp路径
  94. @return 将当前字符串拼接到tmp目录后面
  95. */
  96. - (NSString *)tmpDic
  97. {
  98. NSString *path = NSTemporaryDirectory();
  99. return [path stringByAppendingPathComponent:self.lastPathComponent];
  100. }
  101. /// 检测字符串是否包含中文
  102. +( BOOL)yl_isContainChinese:(NSString *)str
  103. {
  104. for(int i=0; i< [str length];i++)
  105. {
  106. int a = [str characterAtIndex:i];
  107. if( a > 0x4e00 && a < 0x9fff)
  108. {
  109. return YES;
  110. }
  111. }
  112. return NO;
  113. }
  114. /// 整形
  115. + (BOOL)yl_isPureInt:(NSString *)string{
  116. NSScanner* scan = [NSScanner scannerWithString:string];
  117. int val;
  118. return [scan scanInt:&val] && [scan isAtEnd];
  119. }
  120. /// 浮点型
  121. + (BOOL)yl_isPureFloat:(NSString *)string{
  122. NSScanner* scan = [NSScanner scannerWithString:string];
  123. float val;
  124. return [scan scanFloat:&val] && [scan isAtEnd];
  125. }
  126. /// 有效的手机号码
  127. + (BOOL)yl_isValidMobile:(NSString *)str
  128. {
  129. NSString *phoneRegex = @"^1[34578]\\d{9}$";
  130. NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
  131. return [phoneTest evaluateWithObject:str];
  132. }
  133. /// 纯数字
  134. + (BOOL)yl_isPureDigitCharacters:(NSString *)string
  135. {
  136. string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
  137. if(string.length > 0) return NO;
  138. return YES;
  139. }
  140. /// 字符串为字母或者数字
  141. + (BOOL)yl_isValidCharacterOrNumber:(NSString *)str
  142. {
  143. // 编写正则表达式:只能是数字或英文,或两者都存在
  144. NSString *regex = @"^[a-z0-9A-Z]*$";
  145. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  146. return [predicate evaluateWithObject:str];
  147. }
  148. @end