OC基礎知識之字符串NSString與NSMutableString

  • NSMutableString是NSString的子類,即NSString有的方法NSMutableString也有。
  • NSMutableString是可變的字符串,NSString是不可變字符串,即NSMutableString可以對字符串進行增刪改操作,NSString則不可以。

NSString不可變字符串的使用:

1、創(chuàng)建字符串對象

NSString *str1= [NSString stringWithString:@"hello world1"];  //一般不用這種方法
NSLog(@"=====str1:%@",str1);
NSString *str2 = [[NSString alloc] initWithString:@"hello world2"];//一般也不用這種方法
NSLog(@"=====str2:%@",str2);
NSString *str3 = @"hello world3"; //常用方法
NSLog(@"=====str3:%@",str3);
//如果事先不知道字符串的值,可以先初始化一個空字符串,后面再進行賦值
NSString *str4 = nil;
str4 = @"hello world4";
NSLog(@"=====str4:%@",str4);

2、C類型字符串轉化成OC類型字符串,OC類型轉化成C類型

char *s = "hello world";
//參數一 常量字符串 參數二 編碼格式
NSString *str = [[NSString alloc] initWithCString:s encoding:NSUTF8StringEncoding];
NSLog(@"=====str1:%@",str1);
NSString *str1 = @"This is oc";
const char *p = [str1 UTF8String];
NSLog(@"=====p:%s",p);

3、字符串轉化成基本數據類型

NSString *str = @"100";
NSInteger i = str.integerValue; //100
NSString *str2 = @"100.1";
NSInteger f = str2.floatValue;//1oo.1
NSString *str3 = @"1";
BOOL b = str3.boolValue; //YES

4、字符串格式化

NSString *str1 = [NSString stringWithFormat:@"%@%d%@,體重%.1fkg",@"張三身高",180,@"cm",60.5];
//打印結果:======str1張三身高180cm,體重60.5kg
NSLog(@"======str1:%@",str1);

NSString *name = @"張三";
NSInteger age = 20;
NSString *str88 = [NSString stringWithFormat:@"%@的年齡=%d",name,age];

5、字符串長度

NSString *str = @"hello";
NSLog(@"=====length:%d 或者 length:%d",str.length,[str length]);

6、比較2個字符串是否相等

NSString *str1 = @"hello";
NSString *str2 = @"hello2";
if ([str1 isEqualToString:str2]) {
     NSLog(@"相等");
 }else{
     NSLog(@"不相等");
 }

7、比較2個字符串的大小

// 比較結果 NSOrderedSame 相等  NSOrderedAscending 小于 NSOrderedDescending 大于
NSString *str1 = @"student1";
NSString *str2 = @"student2";
NSInteger result = [str1 compare:str2];
if (result == NSOrderedSame) {
    NSLog(@"等于");
} else if(result == NSOrderedAscending){
    NSLog(@"小于");
}else {
    NSLog(@"大于");
}

8、字符串查找

//字符串查找,在str1中能否查找到str2和str3字符串
NSString *str1 = @"NSOrderedSame,NSOrderedAscending,NSOrderedDescending";
NSString *str2 = @"Same";
NSString *str3 = @"Same2";
NSRange range = [str1 rangeOfString:str2];
NSRange range2 = [str1 rangeOfString:str3];
//判斷條件
if (range.location == NSNotFound && range.length == 0) {
     NSLog(@"===1:沒有找到的判斷");
} else {
     NSLog(@"===1:location is %ld length is %ld",range.location,range.length);
}
 if (range2.location == NSNotFound && range2.length == 0) {
    NSLog(@"===2:沒有找到的判斷");
 } else {
    NSLog(@"===2:location is %ld length is %ld",range2.location,range2.length);
 }
//最終輸出
//===1:location is 9 length is 4
//===2:沒有找到的判斷

9、字符串截取

        NSString * str1 = @"張三2010年從北京大學畢業(yè)";
        // 從指定的index開始截取到字符串末尾
        NSString * str2 = [str1 substringFromIndex:str1.length-3];
        NSLog(@"str2 is %@",str2);//str2 is 學畢業(yè)
        
        //從開始位置一直截取到指定的index位置
        NSString *str3 = [str1 substringToIndex:2];
        NSLog(@"str3 is %@",str3);//str3 is 張三
        
        // 截取第三到第八之間的字符(包含第三和第八),注意oc的下標是從0開始計數
        NSString *str4 = [str1 substringWithRange:NSMakeRange(3, 6)];
        NSLog(@"str4 is %@",str4);//str4 is 010年從北

NSMutableString可變字符串的使用

NSMutableString是NSString的子類,所以上面NSString的所有方法NSMutableString都可以使用,下面我們著重講解下可變字符串才擁有的增刪改方法

1、創(chuàng)建可變字符串

        NSMutableString *str = [NSMutableString stringWithString:@"hello"];//靜態(tài)方法
        NSMutableString *str1 = [[NSMutableString alloc] initWithString:@"hello"];//動態(tài)方法
        NSMutableString *str2 = @"hello";//通過這種方法創(chuàng)建的對象是個不可變字符串,使用可變字符串的方法會crash,所以可變字符串一般不使用這種方法創(chuàng)建

2、可變字符串的增刪改方法

        NSMutableString *str = [NSMutableString stringWithString:@"hello"];
        //追加 world
        [str appendString:@"world"];
        NSLog(@"str is:%@",str); //str is:helloworld
        //插入 你好
        [str insertString:@"你好" atIndex:5];//str is:hello你好world
        NSLog(@"str is:%@",str);
        //刪除 你好
//        [str deleteCharactersInRange:NSMakeRange(5, 2)]; 或者
        [str deleteCharactersInRange:[str rangeOfString:@"你好"]];
        NSLog(@"str is:%@",str); //str is:helloworld

NSString與NSMutableString的區(qū)別

1、不可變字符串先后操作的內存空間不一樣 而可變字符串是一致的

        NSMutableString *str = [NSMutableString stringWithString:@"hello"];
        NSString *str2 = @"hello";
        NSLog(@"before str:%@ %p",str,str); 
        NSLog(@"before str2:%@ %p",str2,str2);
        
        [str appendString:@"world"];
        str2 = @"helloworld";
        NSLog(@"after str:%@ %p",str,str);
        NSLog(@"after str2:%@ %p",str2,str2);
        /*
         輸出結果:
        before str:hello 0x103314420
        before str2:hello 0x100005220
        after str:helloworld 0x103314420
        after str2:helloworld 0x1000052a0
       從輸出結果可以看出,不可變字符串先后操作的內存空間不一樣 而可變字符串是一致的
         */

2、深拷貝(mutableCopy)和淺拷貝(copy)的區(qū)別

先看NSString的深淺拷貝

        NSString * string = @"hello world";
        //淺拷貝
        NSString * copyString = [string copy];
        //深拷貝 
        NSMutableString * mutableCopyString = [string mutableCopy];
        NSLog(@"\nstring = %p\ncopyString = %p\nmutableCopyString = %p",string,copyString,mutableCopyString);
            /*
         輸出結果:
         string = 0x100005220
         copyString = 0x100005220
         mutableCopyString = 0x10072dcb0
        */

由此看出NSString中
淺拷貝:未產生新對象
深拷貝:產生新對象

再看NSSMutableString的深淺拷貝

      NSMutableString * string = [NSMutableString stringWithString:@"hello world"];
        //淺拷貝
        NSString * copyMString = [string copy];
        //深拷貝
        NSMutableString * mutableCopyMString = [string mutableCopy];
        NSLog(@"\nmString = %p\ncopyMString = %p\nmutableCopyMString = %p",string,copyMString,mutableCopyMString);
        /*
         輸出結果:
         mString = 0x102900150
         copyMString = 0x102900180
         mutableCopyMString = 0x1029001a0
        */

由此看出NSSMutableString中
淺拷貝:產生新對象
深拷貝:產生新對象

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,681評論 1 32
  • 1.ios高性能編程 (1).內層 最小的內層平均值和峰值(2).耗電量 高效的算法和數據結構(3).初始化時...
    歐辰_OSR閱讀 30,282評論 8 265
  • /*有返回值的方法(給別人一個數據)//有參數函數跟別人要一個數據 公式: - (返回值類型)方法名; 實現: -...
    社會主義頂梁鹿閱讀 341評論 0 0
  • 下面是我最近兩年學習OC中的一些基礎知識,對于學習OC基礎知識的人可能有些幫助,拿出來分享一下,還是那句話不喜勿噴...
    小小趙紙農閱讀 2,831評論 1 7
  • 今天我來到了一個沒有wifi手機靜音,茶水自取,讀書就是回家的麥家理想谷。 我們的故事就是小說 在懵懂的青春我們相...
    光沐思維閱讀 227評論 0 1

友情鏈接更多精彩內容