Objective-C day4 字符串(17-7-28)

NSString

C语言中,字符串是由char(ASCII码)字符组成。

OC中,字符串是由unichar(Unicode)字符组成。

NSString:不可变字符串,即:创建以后,内容和长度不能更改。

NSMutableString:可变字符串,即:创建以后,内容还可以修改。
字符串的创建

    //初始化
    NSString *str = [[NSString alloc] initWithFormat:@"我今年%d岁了",18];
    NSLog(@"%@",str);
    //便利构造器的办法
    NSString *str1 = [NSString stringWithFormat:@"我今年%d岁了",16];
    NSLog(@"%@",str1);
    
    NSString *name = [NSString stringWithFormat:@"nana"];
    NSLog(@"%@",name);

字面量 —— 笑笑语法,语法糖

// @“字符串内容” 得到的是一个字符串常量

    NSString *name1 = @"nana";

NSString的常用方法

NSLog(@"%@的长度为%lu",name1,name1.length);
    unichar c = [name1 characterAtIndex:0];
    NSLog(@"----%c",c);
    
    
    if (name1 == name) {
        NSLog(@"名字相同");
        
    }else {
      NSLog(@"名字bu相同");
    }
    
    
    NSString *name2 =  @"nana";
    if (name1 == name2) {
        NSLog(@"指针地址相同");
        
    }else {
        NSLog(@"指针地址bu相同");
    }
    if ([name1 isEqualToString:name2]) {
        NSLog(@"字符串内容相同");
    }else{
         NSLog(@"字符串内容不相同");
    }
    //字符串的比较
    NSComparisonResult compareResult = [@"abc" compare:@"bcd"];
    NSLog(@"%ld",compareResult);
    
    //从索引值为1的位置开始截取子串(包含1的位置),一直接到最后
    
    NSString *substring =  [@"abcdefg" substringFromIndex:1];
    NSLog(@"%@",substring);
    
     //从字符串开始截取到索引值处(不包含索引值位置)
    NSString *substring1 = [@"abcdefg" substringToIndex:1];
    NSLog(@"%@",substring1);
    
    //截取某一段子串(range代表一个范围,location代表截取到开始位置,length代表截取长度)
    NSString *substring2 = [@"abcdefg" substringWithRange:NSMakeRange(1, 2)];
    NSLog(@"%@",substring2);
    
    //字符串拼接
    //在原有的字符串后面追加一个字符
    NSString *appendString = [@"abc" stringByAppendingString:@"defg"];
    NSLog(@"%@",appendString);
    //在原有的字符串后面追加一个格式化字符串
    NSString *appendString1 = [@"abc" stringByAppendingFormat:@"%d",123];
    NSLog(@"%@",appendString1);
    
    //替换字符串
    //获取jj在contentString中所占的范围
    NSString *contentString = @"nihao.jj";
    NSRange range = [contentString rangeOfString:@"jj"];
    NSLog(@"range: loc = %lu,len = %lu",range.location,range.length);
    if (range.length != 0) {
        //替换该范围的字符串为@“**”
        NSString *repaceString = [@"nihao.jjj" stringByReplacingCharactersInRange:NSMakeRange(3, 3) withString:@"**"];
        NSLog(@"replace:%@",repaceString);
    }
    
    NSString *replaceString = [contentString stringByReplacingOccurrencesOfString:@"kk" withString:@"**"];
    NSLog(@"%@",replaceString);
    
    
    //字符串转int
    NSInteger number = [@"123" intValue];
    NSLog(@"%ld",number +1);
    //其他数据类型转化字符串
    NSLog(@"%@",[NSString stringWithFormat:@"%ld",number]);
    
    //转大写
    NSString *uppercaseString = [@"abc" uppercaseString];
    NSLog(@"%@",uppercaseString);
    
    //转小写
    NSString *lowercaseString = [uppercaseString lowercaseString];
    NSLog(@"%@",lowercaseString);
    
    
    //首字母大写
    NSString *capitalzedString = [@"abc" capitalizedString];
    NSLog(@"%@",capitalzedString);
    
    //是否有前醉
    if ([@"abcdef" hasPrefix:@"abc"]) {
        NSLog(@"abc为该字符串前缀");
    }else{
       NSLog(@"abc NO为该字符串前缀");
    }
    
    //test
    NSString *picURLString = @"http://www.lanou3g.com/icon.png";
    if ([picURLString hasSuffix:@"png"]) {
        //提换
       picURLString = [picURLString stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"];
        
    }else{
        //拼接
        picURLString = [picURLString stringByAppendingString:@".jpg"];
        
    }
    NSLog(@"picURLString:%@",picURLString);
    ```
    >可变字符串的常用方法

    ```c
    //可变字符串
    NSMutableString *str2 = [[NSMutableString alloc] initWithString:@"abcdefg"];
    NSMutableString *string = [NSMutableString stringWithString:@"abcdefg"];
    //在字符串上直接追加字符串
    [str2 appendString:@"hij"];
    NSLog(@"%@",str2);
    //在字符串上直接追加格式化字符串
    [string appendFormat:@"%d",123];
    NSLog(@"%@",string);
    //插入字符串 将一个字符串插入到索引位置处
    [str2 insertString:@"jk" atIndex:1];
    NSLog(@"%@",str2);
    //删除字符串
    [str2 deleteCharactersInRange:NSMakeRange(2, 1)];
    NSLog(@"%@",str2);
    ```
    
    >NSNumber的常用方法
    ```c
    NSNumber *intNumber = [NSNumber numberWithInt:10];
    NSInteger integer = intNumber.integerValue;
    //NSNumber的字面量写法
    NSNumber *number1 = @10;
    int x= 1;
    NSNumber *number2 = @(x);
    ```
    >NSValue 值对象
   ```c
    NSValue *rangeValue = [NSValue valueWithRange:NSMakeRange(10, 10)];
    NSLog(@"%@",rangeValue);
    NSRange range1 = rangeValue.rangeValue;

你可能感兴趣的:(Objective-C day4 字符串(17-7-28))