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];
if (result == NSOrderedAscending) {
NSLog(@"右邊 > 左邊");
}else if(result == NSOrderedSame){
NSLog(@"內容相同");
}else if (result == NSOrderedDescending){
NSLog(@"左邊 > 右邊");
}
//判斷字符串是否以指定字符串開頭
[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];
NSLog(@"%@",NSStringFromRange(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);
//轉為C語言的字符串
const char *s = [str3 UTF8String];
NSLog(@"%s",s);