zoukankan      html  css  js  c++  java
  • OC中常使用的字符串、数组、字典、集合的一些基本方法

    //  使用实例方法创建NSString对象

    //        NSString *str1 = [[NSString alloc] initWithFormat:@"这是一个大晴天"];

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

    //        

    //        NSString *str2 = [[NSString alloc] initWithFormat:@"欢迎你"];

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

    // 

    //        //  使用类方法创建NSString对象

    //        NSString *str3 = [NSString stringWithFormat:@"小李"];

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

    //

    //        //  直接赋值

    //        NSString *str4 = @"ack";

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

    //        

    //        //  获取字符串长度

    //        NSUInteger str1Length = [str1 length];

    //        NSLog(@"%lu", str1Length);

    //        

    //        //  判断是否有前缀

    //        BOOL result1 = [str1 hasPrefix:@"蓝鸥"];

    //        NSLog(@"%d", result1);

    //        

    //        //  判断是否有后缀

    //        BOOL result2 = [str1 hasSuffix:@"科技有限公司"];

    //        NSLog(@"%d", result2);

    //        

    //        //  搜索字符串范围

    //        NSRange range1 = [str1 rangeOfString:@"科技"];

    //        NSLog(@"location %ld, length %ld", range1.location, range1.length);

    //        

    //        //  字符串截取 三种方式

    //        NSString *substr1 = [str1 substringFromIndex:5];

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

    //        

    //        NSString *substr2 = [str1 substringToIndex:3];

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

    //        

    //       // NSRange range2 = {4, 4};

    //        NSString *substr3 = [str1 substringWithRange:NSMakeRange(4, 4)];

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

    //        

    //        //  拼接字符串

    //        NSString *resultStr1 = [str1 stringByAppendingString:str2];

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

    //        

    //        //  替换字符串

    //        NSString *resultStr2 = [str1 stringByReplacingOccurrencesOfString:@"科技有限公司" withString:@"股份公司"];

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

    //        

    //        //  字符串比较

    //        NSComparisonResult result3 = [str1 compare:str2];  //  比较大小

    //        BOOL result4 = [str1 isEqualToString:str2];  //  比较是否相等

    //        NSLog(@"%ld %d", result3, result4);

    //        

    //        //  字符串和数值类型转换

    //        NSString *str8 = [NSString stringWithFormat:@"9.1530"];

    //        NSString *str9 = [NSString stringWithFormat:@"0123"];

    //        double number1 = [str8 doubleValue];

    //        NSInteger number2 = [str9 intValue];

    //        NSLog(@"%lf %ld", number1, number2);

    //        

    //        //  转换大小写

    //        NSString *str5 = [NSString stringWithFormat:@"hello worlD"];

    //        NSString *str6 = [str5 lowercaseString];

    //        NSString *str7 = [str5 uppercaseString];

    //        NSString *str10 = [str5 capitalizedString];  //  单词首字母大写

    //        NSLog(@"%@ %@ %@", str6, str7, str10);

     

    //  可变字符串

    //        NSMutableString *mutableStr1 = [[NSMutableString alloc] initWithFormat:@"蓝鸥科技有限公司"];

    //        

    //        NSMutableString *mutableStr2 = [NSMutableString stringWithFormat:@"欢迎你"];

    //        

    //        //  拼接

    //        [mutableStr1 appendString:mutableStr2];

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

    //        

    //        //  插入

    //        [mutableStr1 insertString:@"3G" atIndex:2];

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

    //        

    //        //  删除

    //        [mutableStr1 deleteCharactersInRange:NSMakeRange(2, 2)];

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

    NSArray

    /*

            //  NSArray数组类

            //  1.使用实例方法创建数组

            NSArray *array1 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];

            NSLog(@"%@",array1);

            //  2.使用类方法创建数组

            NSArray *array2 = [NSArray arrayWithObjects:@"4", @"5", @"6", nil];

            NSLog(@"%@", array2);

            //  3.获取数组元素个数

            NSUInteger count = [array1 count];

            NSLog(@"%lu", count);

            //  4.根据索引值获取对象

            NSString *str = [array1 objectAtIndex:1];

            NSLog(@"%@", str);

            //  5.获取对象在数组中的索引值

            NSUInteger index = [array1 indexOfObject:@"3"];

            NSLog(@"%ld", index);

    */

    /*

            //  NSMutableArray数组类

            //  1.使用实例方法创建数组

            NSMutableArray *array1 = [[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", nil];

            NSLog(@"%@", array1);

            //  2.使用类方法创建数组

            NSMutableArray *array2 = [NSMutableArray arrayWithObjects:@"d", @"e", @"f", nil];

            NSLog(@"%@", array2);

            //  3.添加数组元素

            [array1 addObject:@"x"];

            NSLog(@"%@", array1);

            //  4.插入数组元素

            [array1 insertObject:@"z" atIndex:4];

            NSLog(@"%@", array1);

            //  5.替换数组元素

            [array1 replaceObjectAtIndex:2 withObject:@"y"];

            NSLog(@"%@", array1);

            //  6.删除数组元素

            [array1 removeObject:@"x"];

            NSLog(@"%@", array1);

            //  7.交换指定位置的两个元素

            [array1 exchangeObjectAtIndex:1 withObjectAtIndex:3];

            NSLog(@"%@", array1);

            //  8.根据对象来交换两个元素的位置

            [array1 exchangeObjectAtIndex:[array1 indexOfObject:@"a"] withObjectAtIndex:[array1 indexOfObject:@"z"]];

            NSLog(@"%@", array1);

            //  9.遍历数组对象

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

                NSString *str = [array1 objectAtIndex:i];

                NSLog(@"%@", str);

            }

            //  快速遍历  不用考虑循环次数

            for (NSString *str in array1) {

                NSLog(@"%@",str);

            }

    */

    这里补充一下如何将C语言中的一般数字类型存入OC中的数组,需要用到NSNumber来封装数据

    NSMutableArray *array = [NSMutableArrayarrayWithObjects:@"1", @"2", @"3", nil];

            NSNumber *intNumber = [NSNumber numberWithInteger:4]; 

            [array addObject:intNumber];

    //  将NSnumber转成一般数据类型输出

    NSNumber *result = [array objectAtIndex:3];    

    NSInteger intResult = [result integerValue];

    NSLog(@"%ld", intResult);

    NSDitionary

            //  NSDictionary

            //  使用实例方法创建对象

    /* 

            NSDictionary *dictionary1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", @"value3", @"k3", @"value4", @"adsa", @"v5", @"hk", @"value6", @"zxy", nil];  //  键跟值都必须是对象

            NSLog(@"%@", dictionary1);

            //  使用类方法创建对象

            NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];

            NSLog(@"%@", dictionary2);

            NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];

            NSLog(@"%@", dictionary3);

            //  创建保存所有key的数组

            NSArray *keyArray = [NSArray arrayWithObjects:@"key1", @"key2",@"key3", nil];

            //  创建保存所有value的数组

            NSArray *valueArray = [NSArray arrayWithObjects:@"value1", @"value2", @"value3", nil];

            NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];

            NSLog(@"%@", dictionary4);

    */

    /*

            //  使用文件创建字典对象

            NSDictionary  *dictionary = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lanou3g/Desktop/OC/lesson5/lesson5/dict.plist"];

            NSLog(@"%@", dictionary);

            //  获取字典里面键值对个数

            NSLog(@"%lu", [dictionary count]);

            //  根据key取值

            NSString *value = [dictionary objectForKey:@"key3"];

            NSLog(@"%@", value);

            //  取出所有的key

            NSArray *allKeys = [dictionary allKeys];

            NSLog(@"%@", allKeys);

            //  取出所有value

            NSArray *allValues = [dictionary allValues];

            NSLog(@"%@", allValues);

            //  使用枚举器

            NSEnumerator *enumrator = [dictionary keyEnumerator];

            NSString *str = nil;

            while (str = [enumrator nextObject]) {  //   forin的实现

                NSLog(@"%@", str);

            }

            //  数组中使用枚举器

            NSArray *arr = [NSArray arrayWithObjects:@"vss", @"fdsf", @"gfh", nil];

            NSEnumerator *enumrator2 = [arr objectEnumerator];

            NSString *str2 = nil;

            while (str2 = [enumrator2 nextObject]) {

                NSLog(@"%@", str2);

            }

     */

            //  用于整理对象的拼接

    //        NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1", @"value3", @"key3", nil];

    //        NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value2", @"key2", nil];

    //        [dict1 addEntriesFromDictionary:dict2];

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

            //  删除字典中所有对象

    //        [dict1 removeAllObjects];

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

            //  删除指定key对应的值

    //        NSArray *deleteKeyArray = [NSArray arrayWithObjects:@"key1", @"key2", nil];

    //        [dict1 removeObjectsForKeys:deleteKeyArray];

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

    //  重置字典

    //        [dict1 setDictionary:dict2];

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

            //  添加一个键值对  也可以是根据key修改对应的value

    //        [dict1 setValue:@"value4" forKey:@"key4"];

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

    //        [dict1 setValue:@"value5" forKey:@"key1"];

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

    NSSet

    /*

            //  NSSet   可以理解成字典套着数组

            //  1.使用类方法创建

    //        NSSet *set1 = [NSSet set]; //  创建一个空的集合对象

            NSSet *set2 = [NSSet setWithObject:@"abc"];

            NSLog(@"%@", set2);

            NSSet *set3 = [NSSet setWithObjects:@"aaa", @"bbb", @"ccc", @"ddd", nil];

            NSLog(@"%@", set3);

            //  使用集合创建集合

            NSSet *set4 = [NSSet setWithSet:set3];

            NSLog(@"%@", set4);

            //  使用数组创建集合

            NSArray *arr1 = [NSArray arrayWithObjects:@"eee", @"fff", nil];

            NSSet *set5 = [NSSet setWithArray:arr1];

            NSLog(@"%@", set5);

          

            //  2.使用实例方法创建

            NSSet *set6 = [[NSSet alloc] initWithObjects:@"a", @"b", @"c", @"d", nil];

            NSLog(@"%@", set6);

            NSSet *set7 = [[NSSet alloc] initWithSet:set5];

            NSLog(@"%@", set7);

            NSSet *set8 = [[NSSet alloc] initWithArray:[NSArray arrayWithObjects:@"a", @"b", nil]];

            NSLog(@"%@", set8);

            NSLog(@"%lu", [set8 count]);

            

            //  使用枚举器访问集合元素

            NSEnumerator *enumrator1 = [set6 objectEnumerator];

            NSString *str1 = nil;

            while (str1 = [enumrator1 nextObject]) {

                NSLog(@"%@", str1);

            }

         

            //  判断两个集合是否有交集

            BOOL bool1 = [set6 intersectsSet:set8];

            NSLog(@"%d", bool1);

            //  判断两个集合是否相等

            BOOL bool2 = [set8 isEqualToSet:set7];

            NSLog(@"%d", bool2);

            //  判断当前集合是否有子集

            BOOL bool3 = [set8 isSubsetOfSet:set6];

            NSLog(@"%d", bool3);

            

            //  可变集合

            NSMutableSet *mSet1 = [NSMutableSet setWithObjects:@"111", @"222", @"333", nil];

            NSLog(@"%@", mSet1);

            NSMutableSet *mSet2 = [NSMutableSet setWithArray:[NSArray arrayWithObjects:@"444", @"555", nil]];

            

            //  添加一个对象

            [mSet2 addObject:@"666"];

            NSLog(@"%@", mSet2);

            

            //  将数组对象添加到集合里

            NSMutableSet *mSet3 = [NSMutableSet setWithCapacity:20];

            [mSet3 addObjectsFromArray:[NSArray arrayWithObjects:@"111", @"2", @"3", nil]];

            NSLog(@"%@", mSet3);

     

            //  从集合中删除一个对象

            [mSet1 removeObject:@"333"];

            NSLog(@"%@", mSet1);

            

            //  得到两个集合的交集

            [mSet3 intersectSet:mSet1];

            NSLog(@"%@", mSet3);

            

            //  从一个集合中减去另一个集合

            [mSet1 minusSet:mSet3];

            NSLog(@"%@", mSet1);

            

            //  从一个集合中删除所有元素

    //        [mSet2 removeAllObjects];

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

            

            //  得到两个集合的并集

    //        [mSet1 unionSet:mSet2];

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

            

            //  设置给集合赋值

            [mSet3 setSet:mSet1];

            NSLog(@"%@", mSet3);

            

            //  拼接两个集合

            NSSet *mSet4 = [mSet1 setByAddingObjectsFromSet:mSet2];

            NSLog(@"%@", mSet4);

    */

    字典与数组的嵌套

    //  字典里面放数组

            NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"value1", @"value2", @"value3", nil];

            NSDictionary *dict1 = [NSMutableDictionary dictionaryWithObject:array1 forKey:@"array1"];

            NSLog(@"%@", dict1);

            

            //  数组里面放字典

            NSMutableArray *array2 = [NSMutableArray arrayWithObjects:array1, dict1, nil];

            NSLog(@"%@", array2);

  • 相关阅读:
    五分钟完成 ABP vNext 通讯录 App 开发
    .NET Conf: Xamarin专场会议3.23 开幕
    2020 年 中国.NET开发者调查报告
    推荐一个很棒的开源工作流elsa-core
    尝试使用 Visual Studio Online (Cloud IDE)
    Mongo2Go 介绍
    DevExpress作为企业赞助商加入.NET基金会
    【新书推荐】《ASP.NET Core微服务实战:在云环境中开发、测试和部署跨平台服务》 带你走近微服务开发
    云原生时代 来看看十年前李彦宏、马化腾和马云对云计算的评价
    .NET 在云原生时代的蜕变,让我在云时代脱颖而出
  • 原文地址:https://www.cnblogs.com/Raven930/p/3691045.html
Copyright © 2011-2022 走看看