261 lines
10 KiB
Objective-C
261 lines
10 KiB
Objective-C
//
|
||
// NSString+Regex.m
|
||
// xplan-ios
|
||
//
|
||
// Created by 冯硕 on 2021/9/14.
|
||
//
|
||
|
||
#import "NSString+Utils.h"
|
||
#import <CommonCrypto/CommonDigest.h>
|
||
#import "NSDate+DateUtils.h"
|
||
@implementation NSString (Utils)
|
||
|
||
///是否是正确的手机号
|
||
- (BOOL)isPhoneNumber{
|
||
NSString *regex =@"^((1[3-9][0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
|
||
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
|
||
return [pred evaluateWithObject:self];
|
||
}
|
||
|
||
///MD5加密
|
||
- (NSString *)MD5String {
|
||
const char *cstr = [self UTF8String];
|
||
unsigned char result[16];
|
||
CC_MD5(cstr, (CC_LONG)strlen(cstr), result);
|
||
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]
|
||
];
|
||
}
|
||
|
||
+ (BOOL)versionCompareOldStr:(NSString *)first andNewStr: (NSString *)second{
|
||
if ([first compare:second options:NSNumericSearch] == NSOrderedAscending){
|
||
return NO;
|
||
}else if ([first compare:second options:NSNumericSearch] == NSOrderedSame){
|
||
return YES;
|
||
}else{
|
||
return YES;
|
||
}
|
||
}
|
||
|
||
///数字转化为万
|
||
+ (NSString *)getDealNumwithstring:(NSString *)string{
|
||
if (string.floatValue <10000) {
|
||
return [NSString stringWithFormat:@"%@.0", string];
|
||
}
|
||
NSDecimalNumber *numberA = [NSDecimalNumber decimalNumberWithString:string];
|
||
NSDecimalNumber *numberB = [NSDecimalNumber decimalNumberWithString:@"10000"];
|
||
//NSDecimalNumberBehaviors对象的创建 参数 1.RoundingMode 一个取舍枚举值 2.scale 处理范围 3.raiseOnExactness 精确出现异常是否抛出原因 4.raiseOnOverflow 上溢出是否抛出原因 4.raiseOnUnderflow 下溢出是否抛出原因 5.raiseOnDivideByZero 除以0是否抛出原因。
|
||
NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
|
||
/// 这里不仅包含Multiply还有加 减 乘。
|
||
NSDecimalNumber *numResult = [numberA decimalNumberByDividingBy:numberB withBehavior:roundingBehavior];
|
||
NSString *strResult = [numResult stringValue];
|
||
return [NSString stringWithFormat:@"%@万", strResult];
|
||
}
|
||
///数字转化为万
|
||
+ (NSString *)getDealNumwithstring:(NSString *)string withText:(NSString *)text{
|
||
if (string.floatValue <10000) {
|
||
return [NSString stringWithFormat:@"%@", string];
|
||
}
|
||
if(string.floatValue > 9990000){
|
||
return @"999w+";
|
||
}
|
||
NSDecimalNumber *numberA = [NSDecimalNumber decimalNumberWithString:string];
|
||
NSDecimalNumber *numberB = [NSDecimalNumber decimalNumberWithString:@"10000"];
|
||
//NSDecimalNumberBehaviors对象的创建 参数 1.RoundingMode 一个取舍枚举值 2.scale 处理范围 3.raiseOnExactness 精确出现异常是否抛出原因 4.raiseOnOverflow 上溢出是否抛出原因 4.raiseOnUnderflow 下溢出是否抛出原因 5.raiseOnDivideByZero 除以0是否抛出原因。
|
||
NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:0 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
|
||
/// 这里不仅包含Multiply还有加 减 乘。
|
||
NSDecimalNumber *numResult = [numberA decimalNumberByDividingBy:numberB withBehavior:roundingBehavior];
|
||
NSString *strResult = [numResult stringValue];
|
||
return [NSString stringWithFormat:@"%@%@", strResult,text];
|
||
}
|
||
+ (NSString *)stringWithTimeStamp:(NSString *)timeStamp {
|
||
// 转为秒为单位
|
||
NSTimeInterval second = timeStamp.longLongValue / 1000;
|
||
NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];
|
||
|
||
//把字符串转为NSdate
|
||
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
||
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
|
||
// 时间 10点10分
|
||
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
|
||
[timeFormatter setDateFormat:@"HH:mm"];
|
||
// 日期 2月18号
|
||
NSDateFormatter *dayFormatter = [[NSDateFormatter alloc] init];
|
||
[dayFormatter setDateFormat:@"MM月dd日"];
|
||
// 日期 年月日
|
||
NSDateFormatter *yearFormatter = [[NSDateFormatter alloc] init];
|
||
[yearFormatter setDateFormat:@"YYYY年MM月dd日"];
|
||
|
||
//得到与当前时间差
|
||
NSTimeInterval timeInterval = [date timeIntervalSinceNow];
|
||
timeInterval = -timeInterval;
|
||
|
||
long temp = 0;
|
||
NSString *result;
|
||
|
||
BOOL isSameDay = [[NSCalendar currentCalendar] isDateInToday:date]; // 是否是同一天
|
||
|
||
// A. 当天,且 timeInterval < 1分钟,显示“刚刚”;
|
||
if (timeInterval < 60) {
|
||
return [NSString stringWithFormat:@"刚刚"];
|
||
|
||
// B. 当天,且1分钟≤ timeInterval <60分钟,显示“n分钟前”;
|
||
} else if((temp = timeInterval/60) < 60){
|
||
return [NSString stringWithFormat:@"%ld分钟前",temp];
|
||
|
||
// C. 当天,且n≥60分钟,显示“xx:xx”;
|
||
} else if((temp = temp/60) < 24 && isSameDay){
|
||
return [timeFormatter stringFromDate:date];
|
||
|
||
// C. 非当天,且n≥60分钟,显示“xx:xx”;
|
||
} else if((temp = temp/60) < 24 && !isSameDay){
|
||
return [dayFormatter stringFromDate:date];
|
||
|
||
// D. 跨天,且未跨年,显示“mm-dd”;
|
||
} else if((temp = temp/30) < 30){
|
||
return [dayFormatter stringFromDate:date];
|
||
|
||
} else {
|
||
// E. 跨年,显示“yyyy-mm-dd”;
|
||
return [yearFormatter stringFromDate:date];
|
||
}
|
||
|
||
return result;
|
||
}
|
||
+(NSString *)getCharmImageUrl:(NSString *)url{
|
||
NSString *getUrl = @"1";
|
||
if([url containsString:@"http"]){
|
||
NSArray *charmList = [url componentsSeparatedByString:@"charm_"];
|
||
if(charmList.count == 2){
|
||
NSString *charmUrl = charmList[1];
|
||
NSArray *curCharmList = [charmUrl componentsSeparatedByString:@"."];
|
||
if(curCharmList.count == 2){
|
||
return [NSString stringWithFormat:@"new_charm_%@",curCharmList.firstObject];
|
||
|
||
}
|
||
for (int i = 0;i < 110; i++) {
|
||
NSString *num = i < 9 ? [NSString stringWithFormat:@"0%d",i+1] : [NSString stringWithFormat:@"%d",i+1];
|
||
if([charmUrl containsString:num]){
|
||
getUrl = [NSString stringWithFormat:@"new_charm_%@",num];
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
}else{
|
||
getUrl = [NSString stringWithFormat:@"new_charm_%@",url];
|
||
}
|
||
return getUrl;
|
||
}
|
||
+(NSString *)getWealthImageUrl:(NSString *)url{
|
||
NSString *getUrl = @"1";
|
||
if([url containsString:@"http"]){
|
||
NSArray *wealthList = [url componentsSeparatedByString:@"wealth_"];
|
||
if(wealthList.count == 2){
|
||
NSString *wealthUrl = wealthList[1];
|
||
NSArray *curWealthList = [wealthUrl componentsSeparatedByString:@"."];
|
||
if(curWealthList.count == 2){
|
||
return [NSString stringWithFormat:@"new_exper_%@",curWealthList.firstObject];;
|
||
}
|
||
for (int i = 0;i < 110; i++) {
|
||
NSString *num = i < 9 ? [NSString stringWithFormat:@"0%d",i+1] : [NSString stringWithFormat:@"%d",i+1];
|
||
if([wealthUrl containsString:num]){
|
||
getUrl = [NSString stringWithFormat:@"new_exper_%@",num];
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
}else{
|
||
getUrl = [NSString stringWithFormat:@"new_exper_%@",url];
|
||
}
|
||
return getUrl;
|
||
}
|
||
#pragma mark - 计算星座
|
||
+(NSInteger) getMonth:(long )time
|
||
{
|
||
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000];
|
||
NSCalendar* calendar = [NSCalendar currentCalendar];
|
||
NSDateComponents* components = [calendar components:NSCalendarUnitMonth fromDate:date];
|
||
NSInteger month = components.month;
|
||
return month;
|
||
}
|
||
|
||
+ (NSInteger) getDay:(long) time
|
||
{
|
||
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000];
|
||
NSCalendar* calendar = [NSCalendar currentCalendar];
|
||
NSDateComponents* components = [calendar components:NSCalendarUnitDay fromDate:date];
|
||
NSInteger day = components.day;
|
||
return day;
|
||
}
|
||
|
||
+ (NSString *)calculateConstellationWithMonth:(long)time
|
||
{
|
||
NSString *astroString = @"魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
|
||
NSString *astroFormat = @"102123444543";
|
||
NSString *result;
|
||
|
||
NSInteger month = [self getMonth:time];
|
||
NSInteger day = [self getDay:time];
|
||
|
||
if (month<1 || month>12 || day<1 || day>31){
|
||
return @"错误日期格式!";
|
||
}
|
||
|
||
if(month==2 && day>29)
|
||
{
|
||
return @"错误日期格式!!";
|
||
}else if(month==4 || month==6 || month==9 || month==11) {
|
||
if (day>30) {
|
||
return @"错误日期格式!!!";
|
||
}
|
||
}
|
||
|
||
result=[NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(month*2-(day < [[astroFormat substringWithRange:NSMakeRange((month-1), 1)] intValue] - (-19))*2,2)]];
|
||
|
||
return [NSString stringWithFormat:@"%@座",result];
|
||
}
|
||
|
||
+ (NSString *)calculateAge:(long)time {
|
||
NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000];
|
||
NSCalendar *birthCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
|
||
NSDateComponents *birthCompomemts = [birthCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:date];
|
||
NSInteger year = birthCompomemts.year;
|
||
NSInteger month = birthCompomemts.month;
|
||
NSInteger day = birthCompomemts.day;
|
||
NSLog(@"出生于%ld年%ld月%ld日", year, month, day);
|
||
|
||
NSDate *nowDate = [NSDate date];
|
||
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
|
||
NSDateComponents *compomemts = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
|
||
NSInteger nowYear = compomemts.year;
|
||
NSInteger nowMonth = compomemts.month;
|
||
NSInteger nowDay = compomemts.day;
|
||
NSLog(@"今天是%ld年%ld月%ld日", nowYear, nowMonth, nowDay);
|
||
|
||
// 计算年龄
|
||
NSInteger userAge = nowYear - year - 1;
|
||
if ((nowMonth > month) || (nowMonth == month && nowDay >= day)) {
|
||
userAge++;
|
||
}
|
||
NSLog(@"用户年龄是%ld",userAge);
|
||
return [NSString stringWithFormat:@"%ld", userAge];
|
||
}
|
||
+(NSString *)createUUID{
|
||
|
||
CFUUIDRef uuid;
|
||
CFStringRef uuidText;
|
||
uuid = CFUUIDCreate(NULL);
|
||
uuidText = CFUUIDCreateString(NULL, uuid);
|
||
NSString *time = [NSDate getNowTimeTimestamp];
|
||
NSString *result = [NSString stringWithFormat:@"%@%@",uuidText,time];
|
||
return [result MD5String];
|
||
|
||
}
|
||
@end
|