Files
peko-ios/YuMi/Tools/NSString/NSString+Utils.m

234 lines
10 KiB
Mathematica
Raw Normal View History

2023-07-06 16:54:13 +08:00
//
// NSString+Regex.m
// YUMI
//
// Created by YUMI on 2021/9/14.
//
#import "NSString+Utils.h"
#import <CommonCrypto/CommonDigest.h>
2023-12-07 19:44:52 +08:00
#import "LoginAreaModel.h"
2023-07-06 16:54:13 +08:00
@implementation NSString (Utils)
2023-07-14 18:50:55 +08:00
///
- (BOOL)isPhoneNumber{
2023-08-10 10:12:19 +08:00
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];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
///MD5
2023-07-06 16:54:13 +08:00
- (NSString *)MD5String {
2023-08-10 10:12:19 +08:00
const char *cstr = [self UTF8String];
unsigned char result[16];
CC_MD5(cstr, (CC_LONG)strlen(cstr), result);
return [NSString stringWithFormat:
2023-12-07 19:44:52 +08:00
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
2023-08-10 10:12:19 +08:00
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]
2023-12-07 19:44:52 +08:00
];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
+ (BOOL)versionCompareOldStr:(NSString *)first andNewStr: (NSString *)second{
2023-08-10 10:12:19 +08:00
if ([first compare:second options:NSNumericSearch] == NSOrderedAscending){
return NO;
}else if ([first compare:second options:NSNumericSearch] == NSOrderedSame){
return YES;
}else{
return YES;
}
}
+(NSString *)getDealTenNumWithString:(NSString *)string{
if (string.floatValue < 100000) {
return [NSString stringWithFormat:@"%@", 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:0 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
/// Multiply
NSDecimalNumber *numResult = [numberA decimalNumberByDividingBy:numberB withBehavior:roundingBehavior];
NSString *strResult = [numResult stringValue];
return [NSString stringWithFormat:@"%@W", strResult];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
///
+ (NSString *)getDealNumwithstring:(NSString *)string{
2023-08-10 10:12:19 +08:00
if (string.floatValue < 10000) {
return [NSString stringWithFormat:@"%@", 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:YMLocalizedString(@"NSString_Utils4"), strResult];
2023-07-06 16:54:13 +08:00
}
2023-07-14 18:50:55 +08:00
+ (NSString *)stringWithTimeStamp:(NSString *)timeStamp {
2023-08-10 10:12:19 +08:00
//
NSTimeInterval second = timeStamp.longLongValue / 1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];
2023-12-07 19:44:52 +08:00
2023-08-10 10:12:19 +08:00
//NSdate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
// 1010
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"HH:mm"];
// 218
NSDateFormatter *dayFormatter = [[NSDateFormatter alloc] init];
[dayFormatter setDateFormat:YMLocalizedString(@"NSString_Utils0")];
//
NSDateFormatter *yearFormatter = [[NSDateFormatter alloc] init];
[yearFormatter setDateFormat:YMLocalizedString(@"NSString_Utils1")];
//
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:@"%@", YMLocalizedString(@"NSString_Utils2")];
// B. 1 timeInterval <60n
} else if((temp = timeInterval/60) < 60){
return [NSString stringWithFormat:YMLocalizedString(@"NSString_Utils3"),temp];
// C. n60xx:xx
} else if((temp = temp/60) < 24 && isSameDay){
return [timeFormatter stringFromDate:date];
// C. n60xx: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];
}
2023-12-07 19:44:52 +08:00
2023-08-10 10:12:19 +08:00
return result;
2023-07-06 16:54:13 +08:00
}
2023-09-07 19:01:31 +08:00
+ (NSString *)calculateConstellationWithMonth:(long)time
{
NSString *astroString = YMLocalizedString(@"XPMineHeadView0");
NSString *astroFormat = @"102123444543";
NSString *result;
2023-12-07 19:44:52 +08:00
2023-09-07 19:01:31 +08:00
NSInteger month = [NSString getMonth:time];
NSInteger day = [NSString getDay:time];
2023-12-07 19:44:52 +08:00
2023-09-07 19:01:31 +08:00
if (month<1 || month>12 || day<1 || day>31){
return YMLocalizedString(@"XPMineHeadView1");
}
2023-12-07 19:44:52 +08:00
2023-09-07 19:01:31 +08:00
if(month==2 && day>29)
{
return YMLocalizedString(@"XPMineHeadView2");
}else if(month==4 || month==6 || month==9 || month==11) {
if (day>30) {
return YMLocalizedString(@"XPMineHeadView3");
}
}
2023-12-07 19:44:52 +08:00
2023-09-07 19:01:31 +08:00
result=[NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(month*2-(day < [[astroFormat substringWithRange:NSMakeRange((month-1), 1)] intValue] - (-19))*2,2)]];
2023-12-07 19:44:52 +08:00
2023-09-07 19:01:31 +08:00
return [NSString stringWithFormat:YMLocalizedString(@"XPMineHeadView6"),result];
2023-12-07 19:44:52 +08:00
}
///
+ (NSString *)getCalculateConstellationImageWithMonth:(long)time{
NSString *astroString = YMLocalizedString(@"XPMineHeadView0");
NSString *astroFormat = @"102123444543";
NSString *result;
2023-12-07 19:44:52 +08:00
NSInteger month = [self getMonth:time];
NSInteger day = [self getDay:time];
2023-12-07 19:44:52 +08:00
if (month<1 || month>12 || day<1 || day>31){
return @"";
}
2023-12-07 19:44:52 +08:00
if(month==2 && day>29)
{
return @"";
}else if(month==4 || month==6 || month==9 || month==11) {
if (day>30) {
return @"";
}
}
2023-12-07 19:44:52 +08:00
result=[NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(month*2-(day < [[astroFormat substringWithRange:NSMakeRange((month-1), 1)] intValue] - (-19))*2,2)]];
NSDictionary *starDic = @{YMLocalizedString(@"XPMineHeadView9"):@"pi_capricorn",YMLocalizedString(@"XPMineHeadView10"):@"pi_water_bottle",YMLocalizedString(@"XPMineHeadView11"):@"pi_pisces",YMLocalizedString(@"XPMineHeadView12"):@"pi_aries",YMLocalizedString(@"XPMineHeadView13"):@"pi_taurus",YMLocalizedString(@"XPMineHeadView14"):@"pi_gemini",YMLocalizedString(@"XPMineHeadView15"):@"pi_cancer",YMLocalizedString(@"XPMineHeadView16"):@"pi_lion",YMLocalizedString(@"XPMineHeadView17"):@"pi_virgin",YMLocalizedString(@"XPMineHeadView18"):@"pi_libra",YMLocalizedString(@"XPMineHeadView19"):@"pi_scorpion",YMLocalizedString(@"XPMineHeadView20"):@"pi_shooter"};
return starDic[result];
2023-09-07 19:01:31 +08:00
}
+(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 *)getAgeWithBirth:(long)birth{
NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate dateWithTimeIntervalSince1970:birth/1000]];
NSInteger brithDateYear = [components1 year];
NSInteger brithDateDay = [components1 day];
NSInteger brithDateMonth = [components1 month];
//
NSDateComponents *components2 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]]; NSInteger currentDateYear = [components2 year];
NSInteger currentDateDay = [components2 day]; NSInteger currentDateMonth = [components2 month]; //
NSInteger iAge = currentDateYear - brithDateYear - 1;
if ((currentDateMonth > brithDateMonth) || (currentDateMonth == brithDateMonth && currentDateDay >= brithDateDay)) {
iAge++;
}
return @(iAge).stringValue;
}
2023-12-07 19:44:52 +08:00
///
+(NSString *)getCountryCode{
//
NSString *path = [[NSBundle mainBundle] pathForResource:@"pi_area_info" ofType:@"json"];
//
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
// JSON
NSDictionary *codeData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSArray *codeList = [LoginAreaModel modelsWithArray:codeData[@"RECORDS"]];
NSString *mccCode = [YYUtility getMobileCountryCode];
NSString *countryCode = @"";
if([mccCode isEqualToString:@"65535"])return @"+886";
for (LoginAreaModel *obj in codeList) {
if([mccCode isEqualToString:obj.mcc]){
2023-12-11 16:11:56 +08:00
countryCode = obj.code;
2023-12-07 19:44:52 +08:00
break;
}
}
if(countryCode.length == 0)return @"+886";
return [NSString stringWithFormat:@"+%@",countryCode];
}
2023-07-06 16:54:13 +08:00
@end