NSString+Formatter

2018-09-18  本文已影响28人  秀才不才
电话号过滤
-(NSString *)stringFormaterReplacingStr;
{
    NSString *string = self;
    string = [string stringByReplacingOccurrencesOfString:@"+86" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"(" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@")" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];//这个是中文空格
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];//英文空格
    
    return string;
}
金额
- (NSString *)stringFormatterWithMoney {
    
    NSString *moneyStr = self;
    
    if (!moneyStr || moneyStr.length == 0 ) {
        return @"0.00";
    }
    
    moneyStr = [moneyStr stringByReplacingOccurrencesOfString:@"," withString:@""];
    moneyStr = [moneyStr stringByReplacingOccurrencesOfString:@"," withString:@""];
    
    //应该是绝对值
    if (fabs([moneyStr doubleValue]) < 1000) {
        return  [NSString stringWithFormat:@"%.2f",[moneyStr doubleValue]];
    }
    
    NSNumber *number = [NSNumber numberWithDouble:[moneyStr doubleValue]];
    NSNumberFormatter *format = [[NSNumberFormatter alloc] init];
    [format setPositiveFormat:@",###.00"];
    NSString *resutlStr = [format stringFromNumber:number];
    
    return resutlStr;
    
}
日期
- (NSString *)stringFormatterWithDate:(NSInteger)inputIndex output:(NSInteger)outputIndex {
    
    NSString *dateStr = self;

    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    
    if (inputIndex == 1) {
        [format setDateFormat:@"yyyy"];
    } else if (inputIndex == 2) {
        [format setDateFormat:@"yyyyMM"];
    } else {
        [format setDateFormat:@"yyyyMMdd"];
    }
    
    
    NSDate *date = [format dateFromString:dateStr];
    
    if (outputIndex == 11) {
        [format setDateFormat:@"yyyy年"];
    } else if (outputIndex == 21 || outputIndex == 22) {
        [format setDateFormat:@"yyyy年MM月"];
    } else if (outputIndex == 23 || outputIndex == 24) {
        [format setDateFormat:@"yyyy-MM"];
    }  else if (outputIndex == 31) {
        [format setDateFormat:@"yyyy年MM月dd日"];
    } else if (outputIndex == 32) {
        [format setDateFormat:@"yyyy-MM-dd"];
    } else if (outputIndex == 33) {
        [format setDateFormat:@"yyyy年MM月dd日"];
    }
    
    dateStr = [format stringFromDate:date];
    
    if (outputIndex == 22 || outputIndex == 24) {
        //月份为整数
        dateStr = [NSString stringWithFormat:@"%@%d%@",[dateStr substringToIndex:5],[[dateStr substringWithRange:NSMakeRange(5, 2)] intValue],outputIndex == 22?@"月":@""];
    }
    
    return dateStr;
}
上一篇下一篇

猜你喜欢

热点阅读