iOS基础知识:Objective-C 之 NSString,NSArray,NSDictionary,NSSet

NSString/NSArray/NSDictionary/NSSet


NSString/NSMutableString
1、NSString 和 C类型的string 互相转换

    //C string to OC string
    const char *CString1 = "hello";
    NSString *OCString1 = [NSString stringWithCString:CString1 encoding:NSUTF8StringEncoding];
    NSLog(@"OCString:%@",OCString1);
    //OC string to C string
    NSString *OCString2 = @"world";
    const char *CString2 = [OCString2 cStringUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"CString:%s",CString2);

2、从文件中读取字符串 和 所字符串写入到文件中(持久化)

    //write nsstring to file
    NSString *str1 = @"write this string to file";
    [str1 writeToFile:@"/Users/apple/desktop/str.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];

    //read nsstring from file
    NSString *str2 = [NSString stringWithContentsOfFile:@"/Users/apple/desktop/str.txt" encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"str2:%@",str2);

3、NSString 里面的常用属性

    NSString *str1 = @"1234";
    //求字符串长度
    NSLog(@"length:%lu",str1.length);
    //求integer数据类型
    NSLog(@"integerValue:%lu",str1.integerValue);
    //此外还可以求int/float/double等其他C语言基础数据类型

    NSString *str2 = @"hEllo woRLd!";
    //全部大写
    NSLog(@"str2_up:%@",str2.uppercaseString);
    //全部小写
    NSLog(@"str2_low:%@",str2.lowercaseString);
    //首字母大写
    NSLog(@"str2_cap:%@",str2.capitalizedString);

4、NSString 常用的方法

    //检查是否以某字符串开头
    NSString *str = @"hello world";
    if([str hasPrefix:@"he"]){
        NSLog(@"str start with he");
    }else{
        NSLog(@"str not start with he");
    }
    //检查是事以某字符串结尾
    if ([str hasSuffix:@"ld"]) {
        NSLog(@"str end with ld");
    }else{
        NSLog(@"str not start with ld");
    }

    //比较两个字符串大小
    NSString *str1 = @"123";
    NSString *str2 = @"234";
    if([str1 compare:str2] == NSOrderedAscending){
        NSLog(@"str1 < str2");
    }else{
        NSLog(@"str1 >= str2");
    }

    //从字符串中截取一段
    NSString *totalStr = @"this is a string";
    //head to index(uninclude index)
    NSLog(@"toIndexStr:%@",[totalStr substringToIndex:7]);
    //index to end(include index)
    NSLog(@"fromIndexStr:%@",[totalStr substringFromIndex:7]);
    NSRange range;
    range.location = 8;
    range.length = 4;
    //location to location+length(include location)
    NSLog(@"rangeStr:%@",[totalStr substringWithRange:range]);

    //字符串的查找和替换
    //found
    NSString *searchStr = @"www.baidu.com";
    NSRange searchRange = [searchStr rangeOfString:@"baIdu" options:NSCaseInsensitiveSearch];
    if (range.location == NSNotFound) {
        NSLog(@"not found");
    }else
        NSLog(@"range:(%lu,%lu)",searchRange.location,searchRange.length);
    //replace
    NSLog(@"replace Str:%@",[searchStr stringByReplacingOccurrencesOfString:@"baidu" withString:@"sina"]);

    //删除首尾特殊字符
    NSString *prettyStr = @"\n\n  hello   \n  ";

    NSLog(@"prettyStr:%@",[prettyStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]);

    //格式化字符串和字符串连接操作
    //format
    NSString *formatStr = [NSString stringWithFormat:@"%@ is %d years old!",@"Weizh",24];
    NSLog(@"formatStr:%@",formatStr);
    //connect
    NSString *connectStr1 = @"hello";
    NSString *connectStr2 = @"world";
    NSString *connectStr = [connectStr1 stringByAppendingString:connectStr2];
    NSLog(@"connectStr:%@",connectStr);

    //字符串的分割(分割成数组)
    NSString *separateStr = @"/Users/apple/desktop/test";
    NSArray *separateArray = [separateStr componentsSeparatedByString:@"/"];
    NSLog(@"array:%@",separateArray);

NSArray/NSMutableArray
常用方法整理:

    NSArray *array = @[@"143",@"13",@"24",@"209",@"96"];
    //寻找某个位置的元素 和 查找某个元素的位置
    NSLog(@"obj:%@",array[3]);
    NSLog(@"idx:%lu",[array indexOfObject:@"299"]);

    //从原数组截取部分元素作为新数组 和 从原数组增加一个元素作为新数组
    NSArray *newArray = [array arrayByAddingObject:@"100"];
    NSLog(@"newArray:%@",newArray);
    newArray = [array subarrayWithRange:NSMakeRange(1, 3)];
    NSLog(@"newArray:%@",newArray);

    //将数组中的字符串拼接起来作为一个字符串返回
    NSString *str = [array componentsJoinedByString:@"-"];
    NSLog(@"str:%@",str);

    //数组数据的持久化操作(该方法需要检查是否保存失败)
    [array writeToFile:@"/Users/apple/desktop/array.txt" atomically:YES];

    //数组的排序
    //sorted by string
    newArray = [array sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"newArray:%@",newArray);
    //sorted by string integerValue
    newArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        if ([obj1 integerValue]<[obj2 integerValue]) {
            return NSOrderedAscending;
        }else if([obj1 integerValue]>[obj2 integerValue]){
            return NSOrderedDescending;
        }else
            return NSOrderedSame;
    }];
    NSLog(@"newArray:%@",newArray);

    //可变数组增加元素,向指定索引插入元素,向指定索引替换元素,删除指定索引元素,删除最后一个元素
    NSMutableArray *mutableArray = [NSMutableArray array];
    //add object
    [mutableArray addObjectsFromArray:array];
    NSLog(@"mut-array:%@",mutableArray);
    //insert object
    [mutableArray insertObject:@"50" atIndex:3];
    NSLog(@"mut-array:%@",mutableArray);
    //replace object
    [mutableArray replaceObjectAtIndex:2 withObject:@"20"];
    NSLog(@"mut-array:%@",mutableArray);
    //delete object
    [mutableArray removeObjectAtIndex:2];
    NSLog(@"mut-array:%@",mutableArray);
    //delete last object
    [mutableArray removeLastObject];
    NSLog(@"mut-array:%@",mutableArray);

NSDictionary/NSMutableDictionary

NSDictionary *dic = @{@"name":@"weizh",@"age":@"24"};
    //返回字典键值对个数
    NSLog(@"count:%lu", dic.count);
    //所有key值重建数组
    NSArray *keyArray = [dic allKeys];
    NSLog(@"keyArray:%@",keyArray);
    //所有value值重建数组
    NSArray *valueArray = [dic allValues];
    NSLog(@"valueArray:%@",valueArray);
    //取某个Key的value值
    NSLog(@"%@", dic[@"name"]);
    //以下两种方法可以在可变字典中增加键值对 和 移除键值对
    NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithDictionary:dic];
    mutableDic[@"gender"] = @"male";
    NSLog(@"mutableDic:%@",mutableDic);

    [mutableDic removeObjectForKey:@"age"];
    NSLog(@"mutableDic:%@",mutableDic);

NSSet/NSMutableSet

    NSSet *set = [NSSet setWithObjects:@"123",@"^^",@"\\", nil];
    NSMutableSet *mutSet = [NSMutableSet setWithSet:set];
    //add obj to mutable set
    [mutSet addObject:@"//"];
    NSLog(@"mutSet:%@",mutSet);
    //并差交集
    //union set
    NSMutableSet *mutSet1 = [NSMutableSet setWithObjects:@"ab",@"cd",@"ef", nil];
    NSMutableSet *mutSet2 = [NSMutableSet setWithObjects:@"cd",@"hello",@"world", nil];
    [mutSet1 unionSet:mutSet2];
    NSLog(@"mutSet1:%@",mutSet1);
    //minus set
    mutSet1 = [NSMutableSet setWithObjects:@"ab",@"cd",@"ef", nil];
    mutSet2 = [NSMutableSet setWithObjects:@"cd",@"hello",@"world", nil];
    [mutSet1 minusSet:mutSet2];
    NSLog(@"mutSet1:%@",mutSet1);
    //intersect set
    mutSet1 = [NSMutableSet setWithObjects:@"ab",@"cd",@"ef", nil];
    mutSet2 = [NSMutableSet setWithObjects:@"cd",@"hello",@"world", nil];
    [mutSet1 intersectSet:mutSet2];
    NSLog(@"mutSet1:%@",mutSet1);
    //mutSet2的元素替换掉mutSet1内所有元素
    [mutSet1 setSet:mutSet2];
    NSLog(@"mutSet1:%@",mutSet1);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值