• 学习ios之二NSStringNSNumber


    //-------------------        NSNumber
            //创建一个NSNumber
            NSNumber * monthNumber=[NSNumber numberWithInt:10];
            NSNumber * lengthNumber=[[NSNumber alloc]initWithFloat:10.8];
            NSLog(@"monthNumber:%@",monthNumber);
            NSLog(@"lengthNumber:%@",lengthNumber);
            
            
            //还原成基本类型
            int month=[monthNumber intValue];
            float length=[lengthNumber floatValue];
            NSLog(@"month:%d",month);
            NSLog(@"length:%1f",length);
            
            //-------------------        NSString
            
            //创建空字符串
            NSString * string1=@"这是一个字符串常量";
            NSString * string2=[[NSString alloc]init];
            NSString * string3=[NSString string];
            
            NSLog(@"string1 %@",string1);

            NSLog(@"string2 %@",string2);

            NSLog(@"string3 %@",string3);

            //常量区
            NSString * string4=[[NSString alloc]initWithString:@"abc"];
            NSString * string5=[NSString stringWithString:string4];
            
            NSLog(@"string4 %@",string4);
            NSLog(@"string5 %@",string5);
            
            //堆区
            NSString * string6=[[NSString alloc]initWithFormat:@"%d",10];
            NSString *string7=[NSString stringWithFormat:@"%f",10.8];
            
            NSLog(@"string6 %@",string6);
            NSLog(@"string7 %@",string7);

            //内容是否相同
            if ([string4 isEqualToString:string5]) {
                NSLog(@"same");
            }
            else{
                NSLog(@"different");
            }
            
            //同一个对象
            if (string1==string2) {
                NSLog(@"y");
            }
            
            //比较字符串大小
            //NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending
            NSLog(@"%ld",[string4 caseInsensitiveCompare:string5]);
            
            //字符串长度
             NSLog(@"%ld",[string4 length]);
            
            //改变字符串大小写
            NSString *hello=@"hello WORLD";
            NSLog(@"%@",[hello uppercaseString]);//全部大写
            NSLog(@"%@",[hello lowercaseString]);//   小写
            NSLog(@"%@",[hello capitalizedString]);//首字母大写
            
            //字符串转--基本数据类型
            NSString * string11=@"3.3333";
            NSLog(@"%d",[string11 boolValue]);
            NSLog(@"%f",[string11 floatValue]);
            NSLog(@"%f",[string11 doubleValue]);
            NSLog(@"%d",[string11 intValue]);
            
            //字符串转数组
            NSString * string12=@"abc ddd eee";
            NSArray *array=[string12 componentsSeparatedByString:@" "];
            NSLog(@"%@",array);
            
            //截取字符串
            NSString *string13=[NSString stringWithFormat:@"abcdef"];
            //从字符串的开头一直截取到指定位置,但不包括该位置的字符
            NSString * string14=[string13 substringToIndex:2];//ab
             NSLog(@"string14 %@",string14);
            //以指定位置开始(包括指定位置的字符),并包括之后的全部字符串
            NSString * string15=[string13 substringFromIndex:2];
             NSLog(@"string15 %@",string15);//cedf
            //根据范围截取字符串
            NSRange range={4,2};
    //        range.location=4;
    //        range.length=2;
            NSString *string16=[string13 substringWithRange:range];
            NSLog(@"string16 %@",string16);//ef
            
            //拼接字符串
            NSString * string17=@"1",*string18=@"2";
            NSString *string19=[[NSString alloc]initWithFormat:@"拼接:%@ and %@",string17,string18];
             NSLog(@"string19 %@",string19);
            NSString* string20=[string17 stringByAppendingFormat:@"%@",string18];
            NSString *string21=[string17 stringByAppendingString:string18];
              NSLog(@"string20 %@",string20);
              NSLog(@"string21 %@",string21);
            
            //查询字符串
            NSString *sting22=@"skjdlfjskldfaaaajsldjfklsjdfsjkajdkf";
            NSRange range11=[sting22 rangeOfString:@"aaaa"];
            if (range11.location!=NSNotFound) {
                NSLog(@"找到");
            }

            
            //可变字符串 继承NSString
            //插入
            NSMutableString *str=[NSMutableString stringWithString:@"skjlfjksljdkf"];
            [str insertString:@"11111" atIndex:0];
            NSLog(@"%@",str);
            //删除
            [str deleteCharactersInRange:NSMakeRange(1,2)];
            NSLog(@"%@",str);
     
            //替换
            [str replaceCharactersInRange:NSMakeRange(0, 2) withString:@"45"];
            NSLog(@"%@",str);

  • 相关阅读:
    Oracle关于快速缓存区应用原理
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
    速记const 指针与指向const的指针
    bzoj3992【SDOI2015】序列统计
    ubuntu 1204 安装mysql
    解决getOutputStream() has already been called for this response
    把一个project相关的jar放到project的lib文件夹中
    解决eclipse偶尔无视breakpoint的行为
    解决eclipse无法打开:Failed to load the JNI shared library
    挣值管理公式集合
  • 原文地址:https://www.cnblogs.com/jasonduan/p/2829564.html
Copyright © 2020-2023  润新知