字符串、数组、字典的常用方法

NSString *str1 = [[NSString alloc]initWithFormat:@"dwdaw"];

    //NSString *str2 = [NSString stringWithFormat:@"%d  %d",1222,8888];

NSString *str2 = @"abcdefg";//创建字符串

    NSLog(@"%@",str2);//打印字符串

    NSLog(@"%ld",[str2 length]);//打印字符串长度;

    BOOL result = [str2 hasPrefix:@"sa"];//是否有前缀;

    BOOL jieguo2 = [str2 hasSuffix:@"as"];//是否有后缀;

    BOOL jieguo3 = [str1 isEqualToString:str2];//两个字符串是否相同

    NSLog(@"%@", result ? @"YES" : @"NO");

    NSLog(@"%@", jieguo2 ? @"YES" : @"NO");

    NSLog(@"%@",jieguo3 ? @"相同":@"不相同");

    NSLog(@"%@",[str2 substringFromIndex:4]);//从第4个开始取值到最后

    NSLog(@"%@",[str2 substringToIndex:4]);//从第0到第3取值

    NSRange ranger = NSMakeRange(2, 3);

    NSLog(@"%@",[str2 substringWithRange:ranger]);//从下标为2的开始取后3

    NSString *newStr = [str2 stringByAppendingFormat:@"%d",10000];

    NSLog(@"%@",newStr);//在后边添加字符串(需要规定类型)

    NSString *newStr1 = [str2 stringByAppendingString:@"101"];

    NSLog(@"%@",newStr1);//直接提供字符串添加

    NSString *newStr3 = [str2 stringByReplacingOccurrencesOfString:@"b" withString:@"B"];

    NSLog(@"%@",newStr3);//字符串的替换

    NSLog(@"%@",[str2 uppercaseString]);//转换为大写

    NSLog(@"%@",[str2 lowercaseString]);//转换为小写

    NSLog(@"%@",[str2 capitalizedString]);//首字母大写

    

    

    

    

//-------------------可变字符串-----------------------

    NSMutableString *Str = [[NSMutableString alloc]init];

    [Str appendFormat:@"hijklmn"];

    [Str appendString:@"opqrst"];//字符串拼接

    [Str deleteCharactersInRange:NSMakeRange(0,2)];//删除字符,从下标开始,顺序删2

    [Str insertString:@"你好hi" atIndex:0];//插入字符,在下标之前插入

    [Str replaceCharactersInRange:NSMakeRange(0, 2) withString:@"您好"];//给定范围替换字符;

    NSLog(@"%@",Str);

//---------容器(NSArrayNSDictionaryNSSet-------

    

    

//---------NSArray数组----------

    NSArray *array1 = @[@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"g"]; //语法糖

    NSArray *array2 = [NSArray arrayWithObjects:@"Hello",@"Work", nil];//变比构造器方法;

    NSLog(@"%@ %@",array1,array2);

    NSLog(@"%ld",[array1 count]);//获取数组的元素个数;

    NSLog(@"%@",[array1 objectAtIndex:1]);//通过下标获取数组里的对象;

    

    NSArray *text = [array1 subarrayWithRange:NSMakeRange(2, 2)];

    NSLog(@"****************%@",text);

    

    

  

    

    NSLog(@"%ld",[array1 indexOfObject:@100]);//通过对象获取下标;

    for (int i = 0; i < [array1 count]; i ++) {

        NSLog(@"%@",[array1 objectAtIndex:i]);

    }                                          //打印数组所有元素;

    NSString *string1 = @"hello,hehe,haha,heihei";

    NSArray *array3 = [string1 componentsSeparatedByString:@","];

    NSLog(@"3个数组:%@",array3);//将数组按照分割存入一个数组中

    NSMutableArray *array4 = [NSMutableArray array];

    [array4 addObject:@"LittleBaby"];//给数组添加一个对象;

    [array4 insertObject:@"Hello" atIndex:0];//在下标位置前插入一个对象;

    [array4 replaceObjectAtIndex:0 withObject:@"NiHao"];//根据下标替换

    [array4 exchangeObjectAtIndex:1 withObjectAtIndex:0];//根据下标进行交换

    [array4 removeLastObject];//删除最后一个对象

    NSLog(@"%@",array4);

//---------NSDictionary字典----------键不能重复出现

    

    //创建键

    NSArray *keys = @[@"name",@"age",@"gender"];

    //创建值

    NSArray *value = @[@"Lily",@22,@"girl"];

    //创建字典;需要注意两个数组中得元素个数必须要一样,且对应。因为字典上是依附于 键值对存在

    NSDictionary *dic1 = [[NSDictionary alloc]initWithObjects:value forKeys:keys];

    NSLog(@"%@",dic1);//打印字典;

    NSLog(@"%ld",[dic1 count]);//获取字典中键值对的个数;

    NSLog(@"%@",[dic1 allKeys]);//获取字典中的值;

    NSLog(@"%@",[dic1 allValues]);//获取字典中的键;

    NSLog(@"%@",[dic1 objectForKey:@"name"]);//根据键来获取值

    NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithDictionary:dic1];//将不可变字典转换成可变字典

    [dic2 setObject:@"China" forKey:@"address"];//添加键值对(先值后键);

    [dic2 removeObjectForKey:@"address"];//根据键 来删除键值对

    //removeAllObject;删除所有键值对

    NSLog(@"%@",dic2);

    

//---------------NSet集合-----------------

    NSSet *set1 = [[NSSet alloc]initWithObjects:@1,@2,@3,@2,@1, nil];

    NSLog(@"%@",set1); //无序,并且删掉重复的

    //使用数组对象来创建集合

    NSArray *arr1 = @[@2,@3,@2,@6,@6,@5];

    NSSet *set2 = [[NSSet alloc]initWithArray:arr1];

    NSLog(@"%@",set2);//这个时候,集合可以过滤掉数组中相同的对象。不是因为改变了数组,而是集合的对象;

    NSLog(@"%ld",[set1 count]);//获取集合中对象的个数;

    NSLog(@"%@",[set1 containsObject:@2] ? @"YES" : @"No");//判断一个对象时否在集合当中;

    //可变集合:

    NSMutableSet *set3 = [NSMutableSet setWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];

    [set3 removeObject:@"1"];//删除单个

   // [set3 removeAllObjects];//删除全部

    NSLog(@"%@",set3);

//---------------遍历-----------------

//专门遍历容器:

    for (id object in set3) {

        NSLog(@"%@",object);

    }             //遍历集合;

    for (NSString *key in dic1) {

        NSLog(@"%@ : %@",key,[dic1 objectForKey:key]);

    }              //遍历字典

    for (id object1 in array1) {

        NSLog(@"%@",object1); //遍历数组

    }

//-------------Block-------------(相当于函数)

    /*

    void(^myblock)(void) = ^(void){

        // 大括号里边放的是·功能代码块

        NSLog(@"嘿嘿;blocl");

    };

返回值(^命名)(参数类型) = ^(参数){

    功能代码;

}

    */

    NSInteger(^oneBlock)(NSString *,NSString *)=^(NSString *str11,NSString *str12){

        NSLog(@"比较结果为%ld",[str11 compare: str12]);

        return [str11 compare:str12];

    };

    oneBlock(@"abe",@"abef");

//-------------数组排序-------------

    NSArray *array5 = @[@3,@2,@33,@42,@4343,@7446,@433];

    NSLog(@"%@",[array5 sortedArrayUsingSelector:@selector(compare:)]);//默认是升序排列

    //-------降序--------

    NSMutableArray *array6 = [NSMutableArray arrayWithArray:array5];

        //NSLog(@"%@",array6);

    for (int i = 0; i < [array6 count]-1; i ++) {

        for (int j = 0; j < [array6 count]- 1 - i; j ++) {

            if ([array6[j] compare: array6[j+1]] == -1) {

                [array6 exchangeObjectAtIndex: j withObjectAtIndex:j+1];

            }

        }

    }

    for (NSString *object in array6) {

        NSLog(@"%@",object);

    }

//----------------数组排序和block的结合使用-------------------

    NSArray *array7 = @[@"das",@"gfs",@"gsaf",@"gsafs",@"fdsaf",@"fdsf"];

    NSLog(@"原来数组对象顺序为:%@",array7);

    //定义block对象:

    // NSComparator是系统提供的block排序

    // NSComparator的来源,被系统定义。我们只需要拿来用

    NSComparator sortBlock = ^(id string1,id string2){

        return  [string1 compare:string2];

    };

    NSArray *sortArray = [ array7 sortedArrayUsingComparator:sortBlock];

    NSLog(@"升序排序:%@",sortArray);

    NSMutableArray *_mutableArray = [[NSMutableArray alloc]initWithArray:array7];

    //可变数组排序

    NSLog(@"可变数组排序");

    //sortedArrayUsingComparator这个方法是不需要返回值的,因为可变数组是可以改变自己本身的。

    [_mutableArray sortUsingComparator:^NSComparisonResult(id obj1 , id obj2){

        return  [obj1 compare:obj2];

    }];

    NSLog(@"%@",_mutableArray);

 

//---------NSDate----------

    NSDate *newDate = [NSDate date];//当前时间;

    NSLog(@"%@",newDate);

    NSLog(@"新的时间是:%@",[NSDate dateWithTimeIntervalSinceNow:+8*3600]);//根据当前时间来计算新的时间 — +

    NSLog(@"新的时间是:%@",[NSDate dateWithTimeIntervalSince1970:24*3600*365*45]);//根据给定的时间24*3600*365*45来计算距离1970年的秒数

    NSDate *newDate1 = [NSDate dateWithTimeIntervalSinceNow:+8*3600];

    NSLog(@"较早的时间是:%@",[newDate earlierDate: newDate1]);//获取两个时间较早的一个

    NSLog(@"较晚的时间是:%@",[newDate laterDate: newDate1]);//获取两个时间较晚的一个

    BOOL result1 = [newDate isEqualTo:newDate1];

    NSLog(@"结果为%@",result1 ? @"YES" : @"NO");      //判断两个时间是否一样

    NSTimeInterval timeInterval = [newDate timeIntervalSince1970];

    NSLog(@"%.2f",timeInterval);                     //计算当前时间到1970年的时间戳

    NSDateFormatter *newDate2 = [[NSDateFormatter alloc]init];

    [newDate2 setDateFormat:@"yyy-M-d hh:mm:ss"];      //自定义时间格式     非自定义:NSDateFormatter

    NSString *newDate3 = [newDate2 stringFromDate:newDate];

    NSLog(@"%@",newDate3);


转载于:https://my.oschina.net/wangbin618/blog/489566

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值