zoukankan      html  css  js  c++  java
  • 一些封装好的代码方法块160327

    代码段一:

    - (NSString*)repalaceStringBeforStr:(NSString*)beforStr useReplaceStr:(NSString*)maskStr withPosition:(NSRange)range{
        NSInteger location = range.location;
        NSInteger length = range.length;
        NSString *firstStr = [beforStr substringToIndex:location];
        NSString *lastStr = [beforStr substringFromIndex:length];
        NSString *middleStr = @"";
        for (int i = 0; i < beforStr.length - (length - location); i++) {
            middleStr = [middleStr stringByAppendingString:maskStr];
        }
        NSString *finalStr =[[firstStr stringByAppendingString:middleStr] stringByAppendingString:lastStr];
        return finalStr;
    }
    View Code

    以上代码方法的使用:

        NSString * str = @"6666666666666666";

        NSInteger starLocation = 4;//开始位置

        NSInteger length = str.length - starLocation;//替换长度

        NSRange range =NSMakeRange(starLocation, length);

        NSString * newStr = [self repalaceStringBeforStr:str useReplaceStr:@"*" withPosition:range];

    打印结果为: newStr = 6666********6666

    使用注意事项:range 里面的数据范围不能超出字符串的范围了。

     

     代码段二

    - (NSString*)repalaceStringBeforStr:(NSString*)beforStr useReplaceStr:(NSString*)maskStr atIndex:(NSInteger)index{
        //index 是字符串的倒数下标位置
        NSInteger length = beforStr.length;
        NSInteger subLength = length-index;
        NSString *lastStr = [beforStr substringFromIndex:subLength];
        NSString *middleStr = @"";
        for (int i = 0; i < subLength; i++) {
            middleStr = [middleStr stringByAppendingString:maskStr];
            if (3 == i%4) {
                middleStr = [middleStr stringByAppendingString:@" "];
            }
        }
        NSString *finalStr =[middleStr stringByAppendingString:lastStr];
        return finalStr;
    }
    View Code

        NSString * str = @"6666666666666666";

        NSString * newStr = [self repalaceStringBeforStr:str useReplaceStr:@"*" atIndex:4];

    打印结果为:newStr =   **** **** **** 6666

    说明:最后一个参数是倒数的字符串下标

     

    代码段三

    //输入字符串为:@"20160304145824" 返回数组 [周几,日期,长时间格式,长时间格式(包括周几),月份,年月]
    - (NSArray *)receDateArrWith:(NSString *)timeStr{
        
        //首先拆分与拼接(得到固定格式的时间:yyyy年MM月dd日 HH时mm分ss秒)
        NSString *yyStr = [[timeStr substringToIndex:4] stringByAppendingString:@""];
        NSString *mothStr = [[timeStr substringWithRange:NSMakeRange(4, 2)] stringByAppendingString:@""];
        NSString *ddStr = [[timeStr substringWithRange:NSMakeRange(6, 2)] stringByAppendingString:@""];
        ddStr = [ddStr stringByAppendingString:@" "];
        NSString *hhStr = [[timeStr substringWithRange:NSMakeRange(8, 2)] stringByAppendingString:@""];
        NSString *mmStr = [[timeStr substringWithRange:NSMakeRange(10, 2)] stringByAppendingString:@""];
        NSString *ssStr = [[timeStr substringFromIndex:timeStr.length-2] stringByAppendingString:@""];
        NSString *resultTimeStr = [[[[[yyStr stringByAppendingString:mothStr] stringByAppendingString:ddStr] stringByAppendingString:hhStr] stringByAppendingString:mmStr] stringByAppendingString:ssStr];
        
        //进行时间格式的转化(得到NSDate对象:yyyy-MM-dd HH:mm:ss +0000)
        NSDateFormatter *setDateFormatter = [[NSDateFormatter alloc] init];
        [setDateFormatter setDateFormat:@"yyyy年MM月dd日 HH点mm分ss秒"];
        NSDate *dateFromString = [setDateFormatter dateFromString:resultTimeStr];
        
        //时间格式化
        [setDateFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒 EEE"];
        NSString *tempStr = [setDateFormatter stringFromDate:dateFromString];
        
        NSString *weekStr = [tempStr substringFromIndex:tempStr.length -3];
        NSString *dateStr = [tempStr substringWithRange:NSMakeRange(5, 6)];
        NSString *longTimeStr = [tempStr substringToIndex:tempStr.length -3];//去掉了星期几
        NSString *monthStr = [tempStr substringWithRange:NSMakeRange(5, 3)];
        NSString *yeaMthStr = [tempStr substringWithRange:NSMakeRange(0, 8)];
        
        if ([weekStr isEqualToString:@"Sun"]) {
            weekStr = @"周日";
        }else if ([weekStr isEqualToString:@"Sat"]){
            weekStr = @"周六";
        }else if ([weekStr isEqualToString:@"Mon"]){
            weekStr = @"周一";
        }else if ([weekStr isEqualToString:@"Tue"]){
            weekStr = @"周二";
        }else if ([weekStr isEqualToString:@"Wed"]){
            weekStr = @"周三";
        }else if ([weekStr isEqualToString:@"Thu"]){
            weekStr = @"周四";
        }else if ([weekStr isEqualToString:@"Fri"]){
            weekStr = @"周五";
        }
        
        NSString *llTimeStr = [longTimeStr stringByAppendingString:weekStr];
        
        NSArray *timeAry = @[weekStr,dateStr,longTimeStr,llTimeStr,monthStr,yeaMthStr];
        //做了一个安全的判断
        if (timeAry != nil) {
            return @[weekStr,dateStr,longTimeStr,llTimeStr,monthStr,yeaMthStr];
        }else{
            return nil;
        }
    }
    View Code

        NSString *timeStr = @"20160311145824";

        NSArray *timeAry = [self receDateArrWith:timeStr];

        NSLog(@"获取的时间数组为 %@",timeAry);

     打印结果为:

    获取的时间数组为 [

    周五,

    0311,

    20160311 145824 ,

    20160311 145824周五,

    03,

    201603

    ]

     

     

     

  • 相关阅读:
    CSPS模拟 49
    StrGame
    CSPS模拟 48
    [没有证明]原根求法
    CSPS模拟 47
    CSPS模拟 46
    CSPS模拟 45 乔迁之喜
    CSPS模拟 44
    平衡二叉树
    go语言学习--指针数组和数组指针
  • 原文地址:https://www.cnblogs.com/benpaobadaniu/p/5270252.html
Copyright © 2011-2022 走看看