天天看點

OC中NSString 的常用方法

 NSString *str1 = @"BeiJing";

    NSString *str2 = @"beijing";

    //全部轉為大寫

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

    //全部轉為小寫

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

    //首字母大寫

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

   //比較兩個字元串内容是否相同

    BOOL b =[str1 isEqualToString:str2];

    //兩個字元串内容比較

    //NSOrderedAscending    右邊 > 左邊

    //NSOrderedSame         内容相同

    //NSOrderedDescending   左邊 > 右邊

    NSComparisonResult result = [str1 compare:str2];

    if (result == NSOrderedAscending) {

        NSLog(@"右邊 > 左邊");

    }else if(result == NSOrderedSame){

        NSLog(@"内容相同");

    }else if (result == NSOrderedDescending){

        NSLog(@"左邊 > 右邊");

    }

    //忽略大小寫進行比較,傳回值與compare一樣

    result = [str1 caseInsensitiveCompare:str2];

    //判斷字元串是否以指定字元串開頭

    [str1 hasPrefix:@"aaa"];

    //判斷字元串是否以指定字元串結尾

    [str1 hasSuffix:@"aaa"];

    //判斷字元串是否包含指定字元串,傳回位置和長度

    NSRange range = [@"123456" rangeOfString:@"456"];

    NSLog(@"%@", NSStringFromRange(range));

    //反向搜尋

    range = [@"123456456qweasasd456" rangeOfString:@"456" options:NSBackwardsSearch];

    NSLog(@"%@",NSStringFromRange(range));

    //指定範圍進行搜尋

    range = NSMakeRange(0, 9);

    range = [@"123456456qweasasd456" rangeOfString:@"456" options:NSBackwardsSearch range:range];

       //字元串的截取

        NSString *str = @"123456789";

        NSLog(@"%@",[str substringFromIndex:3]);

        NSLog(@"%@",[str substringToIndex:6]);

        NSLog(@"%@",[str substringWithRange:NSMakeRange(3, 3)]);

        //用指定字元串分割字元串,傳回一個數組

        NSArray *array = [@"1,2,3,4,5,6" componentsSeparatedByString:@","];

        NSLog(@"%@",array);

        //将數組中的字元串組合成一個檔案路徑

        NSMutableArray *components = [NSMutableArray array];

        [components addObject:@"Users"];

        [components addObject:@"CentralPerk"];

        [components addObject:@"Desktop"];

        NSString *path = [NSString pathWithComponents:components];

        NSLog(@"%@",path);  //Users/CentralPerk/Desktop

        //将一個路徑分割成一個數組

        NSArray *array1 = [path pathComponents];

        NSLog(@"%@",array1);

        //判斷是否為絕對路徑(依據:是否以'/'開始)

        path = @"/Users/CentralPerk/Desktop";

        NSLog(@"%i",[path isAbsolutePath]);

        //擷取最後一個目錄

        NSLog(@"%@",[path lastPathComponent]);

        //删除最後一個目錄

        NSLog(@"%@",[path stringByDeletingLastPathComponent]);

        //拼接一個目錄

        NSLog(@"%@",[path stringByAppendingPathComponent:@"aaa"]);     ///Users/CentralPerk/Desktop/aaa

        NSLog(@"%@",[path stringByAppendingString:@"aaa"]);             ///Users/CentralPerk/Desktopaaa

        NSLog(@"%@",[path stringByAppendingFormat:@"%@%@",@"b",@"c"]);  ///Users/CentralPerk/Desktopbc

        //拓展名出來

        //擷取拓展名,不帶.

        NSString *str2 = @"Users/CentralPerk/Desktop/test.txt";

        NSLog(@"%@",[str2 pathExtension]);

        //添加拓展名,不需要帶.

        NSLog(@"%@",[str2 stringByAppendingPathExtension:@"mp3"]);

        //删除拓展名,帶.一塊删除

        NSLog(@"%@",[str2 stringByDeletingPathExtension]);

        //字元串轉為 int double float

        NSString *str3 = @"123";

        NSLog(@"%i",[str3 intValue]);

        NSLog(@"%zi",[str3 length]);

        //取出指定位置的字元

        unichar c = [str3 characterAtIndex:2];

        NSLog(@"%c",c);