object-c 学习之Foundation Kit

        /*--------------通过格式字符串和参数创建字符串--------------*/</span>
        
        NSString *height = nil;
        height = [NSString stringWithFormat:@"The height is %d",4];
        //把height输出
        NSLog(@"%@",height);
        
         /*----取字符串的长度-----*/
        NSInteger length = [height length];
        NSLog(@"%ld",(long)length);
         /*--------字符串比较-------*/
        NSString *firstName = @"power Node 100";
        NSString *anotherName = [NSString stringWithFormat:@"Power Node %d",50];
        //isEquaito 返回一个Bool值来表示两个字符串的内容是否相同(相等)
        if ([firstName isEqualTo:anotherName]) {
            NSLog(@"They are the same");
        }
        
        //两个字符串的比较
        NSString *str1 = @"helloworld";
        NSString *str2 = @"hellowrold";
        BOOL result1 = ([str1 compare:str2] == NSOrderedSame);
        NSLog(@"result1 is %d",result1);
        
        //按字母顺序进行比较
        BOOL result2 = ([str1 compare:str2] == NSOrderedAscending);
        NSLog(@"result1 is %d",result2);
        BOOL result3 = ([str1 compare:str2] == NSOrderedDescending);
        NSLog(@"result1 is %d",result3);
        
//        if (result1 == -1) {
//            NSLog(@"str1小于str2");
//        }
//        if (result1 == 1) {
//            NSLog(@"str1大于str2");
//        }
        
         /*--------创建可变字符串-------*/
        
        NSMutableString *str3 = [NSMutableString stringWithCapacity:20];
        NSMutableString *str4 = [NSMutableString stringWithFormat:@"我今年%d岁",29];
         //往可变字符串末尾追加字符
        [str3 appendString:@"Hello,"];           //在末尾加字符串;
        [str3 appendFormat:@"我是zhan%d",3];      //与stringWithFormat类似,按照格式化字符添加到末尾
        
        [str3 appendString:str4];                //把str4 的内容追加到str3的末尾
        NSLog(@"%@",str3);
        
        /*--------创建可变字符串-------*/
        
        NSMutableString *friend = [NSMutableString stringWithCapacity:50];
        [friend appendString:@"zhangsan lisi wangwu zhaoliu"];
        
        NSRange range = [friend rangeOfString:@"wangwu"];
        //range.length++ ;   //把wangwu后面的空格删掉
        [friend deleteCharactersInRange:range];
        NSLog(@"%@",friend);
        
        /*--------改变字符串的大小写-------*/
        NSString *string11 = @"ASreing";
        NSString *string12 = @"String";
        NSLog(@"after uppercase srting11:%@",[string11 uppercaseString]);
        NSLog(@"after lowercase string12:%@",[string12 lowercaseString]);
        NSLog(@"capitalized string11:%@",[string11 capitalizedString]);
        
        
        /*--------字符串中是否还包含别的字符串-------*/
        
        NSString *fileName = @"draft-chapter.pages";
        if ([fileName hasPrefix:@"draft"]) {
            NSLog(@"This is a draft");
        }
        if ([fileName hasSuffix:@".mov"]) {
            NSLog(@"This is a mov");
        }
        
        
        //-substringFromIndex:从字符串的指定的位置开始(包括位置的字符) 截取
        NSString *string1 = @"This is a string";
        NSString *string2 = [string1 substringFromIndex:3];
        NSLog(@"%@",string2);
        //-substringToindex:从字符串的开头一直截取到指定的位置(但不包括指定位置的字符)
        NSString *string3 = [string1 substringToIndex:5];
        NSLog(@"%@",string3);
        //-substringWithRange:按照range给出的位置和长度来截取子串;
        NSString *string4 =[string1 substringWithRange:NSMakeRange(0, 6)];
        NSLog(@"%@",string4);
        
        
        /*--------创建一个数组-------*/
        NSArray *arr1 = @[@"zhangsan",@"lisi",@"wangwu",@"zhaoliu"];
        // NSArray *arr2 = [NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangwu",@"zhaoliu",nil];
        
        NSLog(@"%@",arr1);   //类似与c语言的格式
        //取对应下标的元素
//        NSString *string10 = [arr1 objectAtIndex:2];
//        NSLog(@"%@",str);
        NSLog(@"%@",arr1[2]);
        
        //数组个数
        
        NSLog(@"The item of arr1 is :%lu",(unsigned long)[arr1 count]);
        
         /*--------创建一个可变数组,并从中删除指定对象-------*/
        NSMutableArray *arr3 = [NSMutableArray arrayWithCapacity:12];
        [arr3 addObject:@"one"];
        [arr3 addObject:@"Two"];
        [arr3 addObject:@"Three"];
        NSLog(@"The element of arr3 is :%@",arr3);
        
        
        //从可变数组中删除对象
        [arr3 removeObject:@"one"];  //删除指定对象
        [arr3 removeObjectAtIndex:0];   //删除下标
        NSLog(@"The element of arr3 is :%@",arr3);
        
        
        [arr3 addObjectsFromArray:arr1];           //把数组arr1追加到arr3
        NSLog(@"The element of arr3 is :%@",arr3);
        [arr3 insertObject:@"insert" atIndex:0];   //在指定位置为arr3添加对象
        NSLog(@"The element of arr3 is :%@",arr3);
        
        //数组的遍历
        
        NSInteger counter = [arr3 count];
        for (int i = 0; i < counter; i++) {
//            NSString *string11 = arr3[i];
            NSString *string11 = [arr3 objectAtIndex:i];
            NSLog(@"The element of arr3 is :%@",string11);
          
        }
        
        /*--------对路径和文件名进行扩展-------*/
        NSString *path = @"~/hello.txt";
        NSString *absolutePath = [path stringByExpandingTildeInPath];
        NSLog(@"absolutepath:%@",absolutePath);
        NSString *tem = [absolutePath stringByAbbreviatingWithTildeInPath];
        NSLog(@"%@",tem);
        
        /*--------快速枚举-------*/
        
        for (NSString *enum1 in arr1) {
            NSLog(@"arr1:%@",enum1);
        }
                          


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值