zoukankan      html  css  js  c++  java
  • Object-C 语法 字符串 数组 字典 和常用函数 学习笔记

    字符串

       //取子字符串
            NSString *str1=@"今天的猪肉真贵,200块一斤";
            
            NSString *sub1=[str1 substringFromIndex:4];
            NSLog(@"%@",sub1);//肉真贵,200块一斤
            
            //区间
            NSRange ran=NSMakeRange(3, 2);
            
            NSString *sub2=[str1 substringWithRange:ran];
            NSLog(@"%@",sub2);//猪肉
            
            //字符串拼接
            NSString *str3=@"进能越塔送人头";
            NSString *str4=@"退可挂机骂队友";
            NSString *newStr=[str3 stringByAppendingString:str4];
            
            NSLog(@"拼接的结果是%@",newStr);
            //字符串替换
            NSString *str5=@"iPhone6";
            NSString *newstr1=[str5 stringByReplacingOccurrencesOfString:@"6" withString:@"7"];
            NSLog(@"%@",newstr1);
            //区间替换
            NSString *newstr2=[str5 stringByReplacingCharactersInRange:NSMakeRange(1, 2) withString:@"66"];
            NSLog(@"%@",newstr2);
            
            
            //大小写
            NSString *str6=@"diaozhatian";
            NSString *newstr3=[str6 uppercaseString];
            NSLog(@"%@",newstr3);
             newstr3=[newstr3 lowercaseString];
            NSLog(@"%@",newstr3);
            
            //判断开头和结尾
            NSString *webStr=@"www.jizhousoft.com";
            //判断开头
            BOOL r1=[webStr hasPrefix:@"www"];
            NSLog(@"%d",r1);//1
            //判断结尾
            BOOL r2=[webStr hasSuffix:@"com"];
            NSLog(@"%d",r2);//1
            
            //是否包含
            NSString *str7=@"我赵日天服了!";
            BOOL r3=[str7 containsString:@"日"];
            if (r3) {
                NSLog(@"请文明聊天.");
                NSString *new3=[str7 stringByReplacingOccurrencesOfString:@"日" withString:@"*"];
                NSLog(@"%@",new3);
            }else {
                NSLog(@"真乖!");
            }
    //可变字符串 相当于  C#的 StringBuilder
            NSMutableString *mStr=[NSMutableString string];
            //初始化为空
    //        NSMutableString *mStr1=[[NSMutableString alloc] initWithCapacity:1];
            [mStr appendString:@"1111"];
            [mStr appendString:@"2222"];
            [mStr insertString:@"666" atIndex:3];
                 NSLog(@"%@",mStr);
            [mStr deleteCharactersInRange:NSMakeRange(3, 3)];
            
            NSLog(@"%@",mStr);
    

    数组

      //不可变数组
            NSArray *arr1=[NSArray arrayWithObjects:@"孙悟空",@"八戒",@"和尚",@"小白龙", nil];
            //字面量
            NSArray *arr2=@[@"孙悟空",@"八戒",@"和尚",@"小白龙"];
            //数组长度
            NSInteger count=[arr1 count];
            NSLog(@"%ld",count);
            
            NSLog(@"%ld",[arr2 count]);
            //数组中获取元素
            NSString *str=[arr2 objectAtIndex:2];
            NSLog(@"%@",str);
            //第一个 最后一个
            str=[arr2 firstObject];
            NSLog(@"%@",str);
            str=[arr2 lastObject];
            NSLog(@"%@",str);
            NSInteger indexof=[arr2 indexOfObject:@"小白龙"];
            NSLog(@"%ld",indexof);
            
            //是否包含
            BOOL r1=[arr1 containsObject:@"小白龙"];
            NSLog(@"%d",r1);//1
            
            //字符串分割成数组
            NSString *str2=@"我勒,个,去";
            NSArray *arr3=[str2 componentsSeparatedByString:@","];
            NSLog(@"%@",arr3);
            
            //数组拼接成字符串
            NSArray *arr4=@[@"123",@"456",@"789",@"147"];
    //        NSArray *arr4=[NSArray arrayWithObjects:  @"123",@"456",@"789",@"147",nil];
            
            NSLog(@"%@",[arr4 componentsJoinedByString:
                         @""]);
            
            
            //可变数组
            NSMutableArray *mArr=[NSMutableArray array];
            
            [mArr insertObject:@"张三" atIndex:0];
            [mArr addObject:@"林黛玉"];
            [mArr addObject:@"李四"];
            for (int i=0; i<[mArr count]; i++) {
                NSLog(@"%@",[mArr objectAtIndex:i]);
            }
            
    //        [mArr removeObjectAtIndex:0];
    //        [mArr removeObject:@"林黛玉"];
    //        for (int i=0; i<[mArr count]; i++) {
    //            NSLog(@"%@",[mArr objectAtIndex:i]);
    //        }
            //数组换位
            [mArr exchangeObjectAtIndex:0 withObjectAtIndex:1];
            //替换
            [mArr replaceObjectAtIndex:0 withObject:@"王五"];
            for (int i=0; i<[mArr count]; i++) {
                NSLog(@"%@",[mArr objectAtIndex:i]);
            }
    

     字典

    int main(int argc, const char * argv[]) {
        // insert code here...
        //不可变字典
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
                           @"哈士奇",@"h",
                           @"柯基",@"k",
                           @"金毛",@"j",
                           nil];
        NSDictionary *dic1=@{@"哈士奇":@"h",
                             @"柯基":@"k",
                             @"金毛":@"j"};
        //获取字典中的元素
        NSString *str=[dic1 objectForKey:@"k"];
        //统计个数
        NSLog(@"%ld",[dic count]);
        
        //获取所有key
        NSArray *arr=[dic1 allKeys];
        
        //获取所有Value
        NSArray *arrValue=[dic allValues];
        
        //可变字典
        
        NSMutableDictionary *mDic=[NSMutableDictionary dictionary];
        [mDic setObject:@"琴女" forKey:@"q"];
        [mDic setObject:@"盲僧" forKey:@"m"];
        [mDic setObject:@"螳螂" forKey:@"t"];
        [mDic setObject:@"淘宝" forKey:@"t"];
        
        //移除t
        [mDic removeObjectForKey:@"t"];
        
        for (NSString *item in [mDic allValues]) {
            NSLog(@"%@",item);
        }
        
        return 0;
    }
    

    集合

    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
      //不可变集合
        NSSet *set1=[NSSet setWithObjects:@"aa",@"22",@"dd",@"gg", nil];
        //集合没有顺序.只能随机读取 而且会自动去除重复
        NSLog(@"%@",set1);
        //获取集合中的元素
     
         NSLog(@"%@",[set1 anyObject]);//随机元素
        
        //可变集合
        NSMutableSet *mSet=[NSMutableSet set];
        [mSet addObject:@"111"];
        [mSet addObject:@"222"];
        [mSet addObject:@"222"];
        [mSet addObject:@"333"];
        [mSet addObject:@"55"];
        NSLog(@"%ld",[mSet count]);//4 去除了重复
        //移除
        [mSet removeObject:@"111"];
        NSLog(@"%ld",[mSet count]);
        
        //计数集合
        NSCountedSet *cSet=[NSCountedSet set];
        [cSet addObject:@"222"];
        [cSet addObject:@"222"];
        [cSet addObject:@"333"];
        [cSet addObject:@"333"];
        [cSet addObject:@"222"];
        [cSet addObject:@"111"];
        [cSet addObject:@"222"];
        NSLog(@"%ld",[cSet countForObject:@"222"]);//4 统计了次数
        
        NSLog(@"%ld",[cSet count]);//3 依然去除了重复
        
        int a=10;
        int b=12;
        //将基本数据类型存入数组
         //先转换为引用类型 字符串
        NSString *astr=[NSString stringWithFormat:@"%d",a];
        NSString *bstr=[NSString stringWithFormat:@"%d",b];
       //再存入数组中
        NSArray *array=[NSArray arrayWithObjects:astr,bstr, nil];
        
        //或者转换为 NSNumber
        float f1=9.44;
        NSNumber *n1=[NSNumber numberWithFloat:f1];
        //存入数组
        NSArray *array1=@[n1];
        //从数组中取出
        NSNumber *resultNumber=array1[0];
        //读取时转换
        float resultFloat=[resultNumber floatValue];
        NSLog(@"%f",resultFloat);
        
        
        return 0;
    }
    

    视频学习地址: https://ke.qq.com/webcourse/index.html#course_id=169598&term_id=100198033&taid=833404044220030&vid=e14148936nh

  • 相关阅读:
    Class StatusesTableSeeder does not exist 如何解决
    Heroku 如何上重置 PostgreSQL 数据库
    git强制push
    Heroku登录失败
    将手机替换为*号
    使用TP自带缓存时。出现第一次拿不到数据。
    PHP实现查看邮件是否被阅读
    使用file_get_contents下载图片
    介绍几款开源好用的产品
    查看光纤卡wwn号【转载】
  • 原文地址:https://www.cnblogs.com/Qbit/p/6243540.html
Copyright © 2011-2022 走看看