NSString NSDate NSArray常用方法

前端之家收集整理的这篇文章主要介绍了NSString NSDate NSArray常用方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

方法声明及注释:

#pragma mark 字符串相关方法

//转换json

-(NSString *)jsonFromObject:(id)obj;

//判断字符串

-(BOOL)isString:(NSString *)string;

//字典赋值

-(NSString *)dictionaryWithDic:(NSDictionary *)dic valueForKey:(NSString *)key;

//string转换成date

-(NSDate *)dateFromString:(NSString *)dateString;

//date转换成string

-(NSString *)stringFromDate:(NSDate *)date;

//时间戳转换成字符串

-(NSString *)stringFromValue:(NSString *)value;

//验证邮箱格式

-(BOOL)isValidateEmail:(NSString *)email;

//验证电话号码

- (BOOL)isValidateMobile:(NSString *)mobileNum;

//判断是否为整形

- (BOOL)isPureInt:(NSString*)string;

//判断是否为浮点形:


- (BOOL)isPureFloat:(NSString*)string;

//保留几位小数

-(NSString *)newFloat:(float)value withNumber:(int)numberOfPlace;

//文本框赋值

-(NSString *)setText:(NSString *)text;

-(NSString *)setNumberText:(NSNumber *)number Unit:(NSString *)unit;

-(NSArray *)arraySorting:(NSString *)parameters Array:(NSArray *)array Methods:(BOOL)methods;//yes 是正序;no是倒序


方法实现:

-(NSString *)jsonFromObject:(id)obj{

NSString *jsonString = nil;

NSError *error;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj

options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string

error:&error];

if (! jsonData) {

NSLog(@"Got an error: %@", error);

} else {

jsonString [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

}

return jsonString;

}

#pragma mark 字符串相关方法

-(BOOL)isString:(NSString *)string{//字符串是否为空

if (![string isKindOfClass:[NSNull class]]&&string&&![string isEqualToString:@"(null)"]&& string.length != 0 &&![string isEqualToString:@"<null>"]) {

return YES;

}else{

return NO;

}

}

-(NSString *)dictionaryWithDic:(NSDictionary *)dic valueForKey:(NSString *)key{//字典取值

NSString * value = nil;

if (dic!=nil&&[[dic allKeys] containsObject:key]) {

value = [dic valueForKey:key];

}else{

value = @"";

}

return value;

}

- (NSDate *)dateFromString:(NSString *)dateString{

NSDateFormatter *dateFormatter [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];

NSDate *destDate= [dateFormatter dateFromString:dateString];

return destDate;

}


- (NSString *)stringFromDate:(NSDate *)date{

init];

//zzz表示时区,zzz可以删除,这样返回的日期字符将不包含时区信息。

[dateFormatter setDateFormat:@"MM-dd HH:mm"];

NSString *destDateString = [dateFormatter stringFromDate:date];

return destDateString;

}

-(NSString *)stringFromValue:(NSString *)value{

NSString *str = [value substringToIndex:10];


NSDate *confromTimesp [NSDate dateWithTimeIntervalSince1970:[str longLongValue]];

NSDateFormatter* formatter alloc]init];

[formatter setDateFormat:@"MM-dd HH:mm"];

NSString *date = [formatter stringFromDate:confromTimesp];

NSDate *now = [NSDate date];

NSString *dateNow = [formatter stringFromDate:now];

if ([[dateNow substringToIndex:5] isEqualToString:[date substringToIndex:5]]) {

date = @" 今天";

}

NSLog(@"当前时间:%@ 加载时间%@ %@",dateNow,0)"> date,[dateNow substringToIndex:5]);

return date;

}

/*邮箱验证 MODIFIED BY HELENSONG*/

-(BOOL)isValidateEmail:(NSString *)email

{

NSString *emailRegex @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

return [emailTest evaluateWithObject:email];

}

/*手机号码验证 MODIFIED BY HELENSONG*/

- (BOOL)isValidateMobile:(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

*/

* MOBILE @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";

10 * 中国移动:China Mobile

11 * 134[0-8],188

12 */

CM @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";

15 * 中国联通:China Unicom

16 * 130,186

17 */

CU @"^1(3[0-2]|5[256]|8[56])\\d{8}$";

20 * 中国电信:China Telecom

21 * 133,33)"> 22 */

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 MOBILE];

NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF CM];

NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF CU];

NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF CT];

if (([regextestmobile evaluateWithObject:mobileNum] == YES)

|| ([regextestcm evaluateWithObject:mobileNum] == YES)

|| ([regextestct evaluateWithObject:mobileNum] == YES)

|| ([regextestcu evaluateWithObject:mobileNum] == YES))

{

return YES;

}

else

{

NO;

}

}


/*车牌号验证 MODIFIED BY HELENSONG*/

BOOL validateCarNo(NSString* carNo)

{

*carRegex @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";

NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF(@"carTest is %@",carTest);

return [carTest evaluateWithObject:carNo];

}

//判断是否为整形

- (BOOL)isPureInt:(NSString*)string{

NSScanner* scan = [NSScanner scannerWithString:string];

int val;

return[scan scanInt:&val] && [scan isAtEnd];

}


//判断是否为浮点形:


- (BOOL)isPureFloat:(NSString*)string{

NSScanner* scan = [NSScanner scannerWithString:string];

float val;

return[scan scanFloat:&val] && [scan isAtEnd];

}

//保留几位小数

-(NSString *)newFloat:(float)value withNumber:(int)numberOfPlace

{

NSString *formatStr = @"%0.";

formatStr = [formatStr stringByAppendingFormat:@"%df", numberOfPlace];

formatStr = [NSString stringWithFormat:formatStr, value];

printf("formatStr %s\n", [formatStr UTF8String]);

return formatStr;

}

-(NSString *)setText:(NSString *)text{

NSString *str = text;

if (str.length==0) {

str = @"未填写";

}

return str;

}

-(NSString *)setNumberText:(NSNumber *)number Unit:(NSString *)unit{

// [self newFloat:[number floatValue] withNumber:1];

if ([number isKindOfClass:[NSNull class]]||[number integerValue]==0) {

number = [NSNumber numberWithInteger:0];

}

NSString *str =@"";

if (unit.length==0) {

str = [NSString stringWithFormat:@"%@",[self newFloat:[number floatValue] withNumber:0]];

}else{

str = [NSString stringWithFormat:@"%@%@",[self newFloat:[number floatValue] withNumber:0],unit];

}

return str;

}

-(NSArray *)arraySorting:(NSString *)parameters Array:(NSArray *)array Methods:(BOOL)methods{//yes是倒序

NSSortDescriptor*sorter=[[NSSortDescriptor alloc]initWithKey:parameters ascending:YES];

NSMutableArray *sortDescriptors=[[NSMutableArray alloc]initWithObjects:&sorter count:1];

NSArray *sortArray=[array sortedArrayUsingDescriptors:sortDescriptors];

NSArray* reversedArray = [[sortArray reverSEObjectEnumerator] allObjects];

if (methods) {

return sortArray;

}else{

return reversedArray;


}

}

原文链接:https://www.f2er.com/regex/360899.html

猜你在找的正则表达式相关文章