1. 程式人生 > >Object-c之不可變字串 常用方法

Object-c之不可變字串 常用方法

    // 01.建立不可變字串

    NSString *str =[NSStringstring]; // 空字串物件

    NSLog(@"str= %@", str);

    NSString *str1 =@"此字串存放於常量區";

    NSLog(@"str1= %@",str1);

    // 輸出: str = () str1 =此字串存放於常量區

    //02.格式化建立字串(類方法)

    NSString *str2 =[NSStringstringWithFormat:@"%s%f","hello", 5.2];

    NSLog(@"str2= %@", str2);

    // 輸出: str1 = hello5.200000

    //03.利用C語言的字串生成OC的字串物件

    NSString *str3 =[NSStringstringWithUTF8String:"welloc中國"];

    NSLog(@"str3= %@", str3);

    // 輸出: str3 = welloc中國

    //04.將一個字串複製到另一字串

    NSString *str4 =[NSStringstringWithString:str3];

    NSLog(@"str4= %@", str4);

    // 輸出: str4 = welloc中國

    //05.

把網址內容生成字串物件

    NSURL *url =[NSURLURLWithString:@"http://fuli.ba"];

    NSString *str5 =[NSStringstringWithContentsOfURL:url encoding:NSUTF8StringEncoding   error:nil];  

    NSLog(@"%@", str5);

    //06.把檔案內容生成字串物件

    NSString *str6 =[NSStringstringWithContentsOfFile:@"/Users/fuck/1.html"encoding:NSUTF8StringEncoding

error:nil];

    NSLog(@"%@", str6);

    /*********************字串相關操作************************/

      NSString *str01= @"hello world";

      NSString *str11= @"hello china";

    //獲取字串長度

    NSLog(@"len= %li",[str01length]);

    // 輸出: len = 11

    //獲取指定位置的字元

    unichar ch =[str01 characterAtIndex:1];

    NSLog(@"ch= %C", ch);// 大寫C

    // 輸出: ch = e

    //提取字串方法(從索引位置提取子串到字串末尾)

    NSString *str02= [str01 substringFromIndex:6];

    NSLog(@"str02= %@",str02);// 0開始

    // 輸出: str02 = world

    //從字串的開始位置提取子串到索引位置,不包含索引位置的字元

    NSString *str03= [str01 substringToIndex:7];

    NSLog(@"str03= %@",str03);

    // 輸出: str03 = hello w

    //提取指定範圍內的字串

    NSString *str04= [str01 substringWithRange:NSMakeRange(6,3)];

    NSLog(@"str04= %@",str04);

    // 輸出: str04 = wor

    //判斷兩個字串是否相等

    BOOL ret1 =[str01 isEqualToString:str11];

    NSLog(@"ret1= %i", ret1);//相等返回yes(1)

    // 輸出: ret1 = 0

    //兩個字串比較

    NSComparisonResult ret2 =[str01compare:str11];

    NSLog(@"ret2= %li", ret2);

    // 輸出: ret2 = 1

    //追加字串(不是直接修改原字串)

    NSString *str05= [str01 stringByAppendingString: @"fuck you world ~ ~ ~"];

    NSLog(@"str05= %@",str05);

    // 輸出: str05 = hello world fuck you world ~ ~~u438

    //格式化追加字串(66也沒改變)

    NSString *str06= [str05 stringByAppendingFormat:@"%c%d", 'u', 438];

    NSLog(@"str06= %@", str06);

    // 輸出: str06 = hello world fuck you world ~ ~ ~

    //正序字串查詢

    NSRange range1= [str05 rangeOfString:@"world"];

    NSLog(@"location1= %li length1 = %li",range1.location,range1.length);

    // 輸出: location1 = 6 length1 = 5

    //倒序字串查詢

    NSRange range2= [str05 rangeOfString:@"world" options:NSBackwardsSearch];

    NSLog(@"location2= %li length2 = %li",range2.location,range2.length);

    // 輸出: location2 = 21 length2 = 5

    //把數字字串轉化成數字

    int a = [@"123"intValue];

    NSLog(@"a= %i",a);

    float b = [@"00.254"floatValue];

    NSLog(@"b= %f", b);

    // 輸出: a =123 b = 0.254000

    //大小寫轉換

    NSLog(@"%@",[@"helloworld"uppercaseString]);

    NSLog(@"%@",[@"HELLOWORLD"lowercaseString]);

    // 輸出: HELLO WORLD hello world

    //把每個單詞的首字母轉換成大寫,其餘小寫

    NSLog(@"%@",[@"hElloworlD"capitalizedString]);

    // 輸出: Hello World

    //字串替換

    NSString *str07= [@"hello world hello world "stringByReplacingOccurrencesOfString:@"hello"withString:@"hi"];

    NSLog(@"str07= %@", str07);

    // 輸出: str07 = hi world hi world

 //以字串整體分割

NSString *str = @" #I #am Prime  # # #Optimus";

NSArray *dstArray = [str componentsSeparatedByString:@" #"];

NSLog(@"dstArray = %@", dstArray);

NSMutableArray *mulArray = [NSMutableArray arrayWithArray:dstArray];

[mulArray removeObject:@""];

NSLog(@"mulArray = %@", mulArray);

//輸出 : dstArray = ("",I,"am Prime","","",Optimus)

//輸出 : mulArray = (I,"am Prime ",Optimus)

//以字符集分割字串

NSArray *dstArray2 = [str componentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"# "] ];

NSLog(@"dstArray2 = %@", dstArray2);

NSMutableArray *mulArray2 = [NSMutableArray arrayWithArray:dstArray2];

[mulArray2 removeObject:@""];

NSLog(@"mulArray2 = %@", mulArray2);

//輸出 : dstArray2 =("","",I,"",am,Prime,"","","","","","",Optimus)

//輸出 : mulArray2 = (I,am,Prime,Optimus)