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]; 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的总结,不清楚的看SDK
NSString *str = @"123456789"; NSLog(@"%@",[str substringFromIndex:3]); NSLog(@"%@",[str substringToIndex:6]); NSLog(@"%@",[str substringWithRange:NSMakeRange(3, 3)]);
1. 用指定字符串分割字符串,返回一个数组
NSArray *array = [@"1,2,3,4,5,6" componentsSeparatedByString:@","]; NSLog(@"%@",array);
三。NSString的path拼接与删除
1.将数组中的字符串组合成一个文件路径
NSMutableArray *components = [NSMutableArray array]; [components addObject:@"Users"]; [components addObject:@"CentralPerk"]; [components addObject:@"Desktop"]; NSString *path = [NSString pathWithComponents:components]; NSLog(@"path = %@",path); //path = Users/CentralPerk/Desktop
2.将一个路径分割成一个数组:
NSArray *array1 = [path pathComponents]; NSLog(@"路径拼接成的array1 = %@",array1); /* 路径拼接成的array1 = ( Users, CentralPerk, Desktop ) */
3. 判断是否为绝对路径(依据:是否以'/'开始):
path = @"/Users/CentralPerk/Desktop"; NSString *path1 = @"Users/CentralPerk/Desktop"; NSLog(@"判断"@"/Users/CentralPerk/Desktop""否是绝对路径%i",[path isAbsolutePath]); NSLog(@"判断"@"Users/CentralPerk/Desktop""否是绝对路径%i",[path1 isAbsolutePath]); // 判断/Users/CentralPerk/Desktop否是绝对路径1 // 判断Users/CentralPerk/Desktop否是绝对路径0
4.获取最后一个目录:
NSLog(@"获取"@"/Users/CentralPerk/Desktop""最后一个目录:%@",[path lastPathComponent]); // 获取/Users/CentralPerk/Desktop最后一个目录:Desktop
5.删除最后一个目录:
NSLog(@"删除"@"/Users/CentralPerk/Desktop""最后一个目录%@",[path stringByDeletingLastPathComponent]); // 删除/Users/CentralPerk/Desktop最后一个目录/Users/CentralPerk
6.拼接一个目录:(与字符串拼接不同)
NSLog(@"拼接"@"/Users/CentralPerk/Desktop""最后一个目录%@",[path stringByAppendingPathComponent:@"aaa"]); // 拼接/Users/CentralPerk/Desktop最后一个目录/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, [str2 pathExtension]); //添加拓展名,不需要带. NSLog(@"给%@添加扩展名后得到的:%@",str2, [str2 stringByAppendingPathExtension:@"mp3"]); //删除拓展名,带.一块删除 NSLog(@"将%@的扩展名删除后得到的是:%@",str2,[str2 stringByDeletingPathExtension]);
输出: 获取 Users/CentralPerk/Desktop/test.txt 的扩展名 txt
给Users/CentralPerk/Desktop/test.txt添加扩展名后得到的:Users/CentralPerk/Desktop/test.txt.mp3
将 Users/CentralPerk/Desktop/test.txt 的扩展名删除后得到的是: Users/CentralPerk/Desktop/test五。字符串转化问题
1.字符串转为 int double float
NSString *str3 = @"123"; NSLog(@"%i",[str3 intValue]); NSLog(@"%zi",[str3 length]);
2. 取出指定位置的字符
unichar c = [str3 characterAtIndex:2]; NSLog(@"%c",c);
3. 转为C语言的字符串
const char *s = [str3 UTF8String]; NSLog(@"%s",s);