123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- //
- // NSString+Utils.m
- // MIT_Endorsement
- //
- // Created by 翟玉磊 on 2018/1/30.
- // Copyright © 2018年 翟玉磊. All rights reserved.
- //
- #import "NSString+Utils.h"
- @implementation NSString (Utils)
- /**
- * 截取URL中的参数
- *
- * @return NSMutableDictionary parameters
- */
- - (NSMutableDictionary *)getURLParameters {
-
- // 查找参数
- NSRange range = [self rangeOfString:@"?"];
- if (range.location == NSNotFound) {
- return nil;
- }
-
- NSMutableDictionary *params = [NSMutableDictionary dictionary];
-
- // 截取参数
- NSString *parametersString = [self substringFromIndex:range.location + 1];
-
- // 判断参数是单个参数还是多个参数
- if ([parametersString containsString:@"&"]) {
-
- // 多个参数,分割参数
- NSArray *urlComponents = [parametersString componentsSeparatedByString:@"&"];
-
- for (NSString *keyValuePair in urlComponents) {
- // 生成Key/Value
- NSArray *pairComponents = [keyValuePair componentsSeparatedByString:@"="];
- NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding];
- NSString *value = [pairComponents.lastObject stringByRemovingPercentEncoding];
-
- // Key不能为nil
- if (key == nil || value == nil) {
- continue;
- }
-
- id existValue = [params valueForKey:key];
-
- if (existValue != nil) {
-
- // 已存在的值,生成数组
- if ([existValue isKindOfClass:[NSArray class]]) {
- // 已存在的值生成数组
- NSMutableArray *items = [NSMutableArray arrayWithArray:existValue];
- [items addObject:value];
-
- [params setValue:items forKey:key];
- } else {
-
- // 非数组
- [params setValue:@[existValue, value] forKey:key];
- }
-
- } else {
-
- // 设置值
- [params setValue:value forKey:key];
- }
- }
- } else {
- // 单个参数
-
- // 生成Key/Value
- NSArray *pairComponents = [parametersString componentsSeparatedByString:@"="];
-
- // 只有一个参数,没有值
- if (pairComponents.count == 1) {
- return nil;
- }
-
- // 分隔值
- NSString *key = [pairComponents.firstObject stringByRemovingPercentEncoding];
- NSString *value = [pairComponents.lastObject stringByRemovingPercentEncoding];
-
- // Key不能为nil
- if (key == nil || value == nil) {
- return nil;
- }
-
- // 设置值
- [params setValue:value forKey:key];
- }
-
- return params;
- }
- /**
- 获取缓存路径
-
- @return 将当前字符串拼接到cache目录后面
- */
- - (NSString *)cacheDic
- {
- NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
-
- return [path stringByAppendingPathComponent:self.lastPathComponent];
- }
- /**
- 获取document路径
-
- @return 将当前字符串拼接到document目录后面
- */
- - (NSString *)docDic
- {
- NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
-
- return [path stringByAppendingPathComponent:self.lastPathComponent];
- }
- /**
- 获取tmp路径
-
- @return 将当前字符串拼接到tmp目录后面
- */
- - (NSString *)tmpDic
- {
- NSString *path = NSTemporaryDirectory();
-
- return [path stringByAppendingPathComponent:self.lastPathComponent];
- }
- /// 检测字符串是否包含中文
- +( BOOL)yl_isContainChinese:(NSString *)str
- {
- for(int i=0; i< [str length];i++)
- {
- int a = [str characterAtIndex:i];
- if( a > 0x4e00 && a < 0x9fff)
- {
- return YES;
- }
- }
- return NO;
- }
- /// 整形
- + (BOOL)yl_isPureInt:(NSString *)string{
- NSScanner* scan = [NSScanner scannerWithString:string];
- int val;
- return [scan scanInt:&val] && [scan isAtEnd];
- }
- /// 浮点型
- + (BOOL)yl_isPureFloat:(NSString *)string{
- NSScanner* scan = [NSScanner scannerWithString:string];
- float val;
- return [scan scanFloat:&val] && [scan isAtEnd];
- }
- /// 有效的手机号码
- + (BOOL)yl_isValidMobile:(NSString *)str
- {
- NSString *phoneRegex = @"^1[34578]\\d{9}$";
- NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
- return [phoneTest evaluateWithObject:str];
- }
- /// 纯数字
- + (BOOL)yl_isPureDigitCharacters:(NSString *)string
- {
- string = [string stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
- if(string.length > 0) return NO;
-
- return YES;
- }
- /// 字符串为字母或者数字
- + (BOOL)yl_isValidCharacterOrNumber:(NSString *)str
- {
- // 编写正则表达式:只能是数字或英文,或两者都存在
- NSString *regex = @"^[a-z0-9A-Z]*$";
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
- return [predicate evaluateWithObject:str];
- }
- @end
|