1.NSString是OC中用来保存字符串的对象,可以通过在C字符串常量前加上一个@符号来创建一个NSString对象,例如
NSString *strTest=@"my name is xy";
2.也可以使用NSSring的方法来创建,例如,其中stringWithFormat的用法和NSLog一样
const char* test="my name is xy";
NSString *strTest=[NSString stringWithFormat:@"%s",test];
NSString *strTest=[NSString stringWithCString:test encoding:NSASCIIStringEncoding];
NSString *strTest=[[NSString alloc] initWithCString:test encoding:NSASCIIStringEncoding];
3.NSString也可以转换为一个C字符串,例如
NSLog(@"%s",[strTest cStringUsingEncoding:NSASCIIStringEncoding]);
4.获取NSString字符串长度,注意,这里%zd对应的数字类型是size_t,代表和机器类型相关的unsigned类型,即和NSUInteger一样,这里也可以使用%ld或者%d
NSLog(@"str length is %zd",[strTest length]);
5.两个NSString对象之间比较是否相同,区分大小写,相同则返回true
NSLog(@"str cmp result is %@",[strTest isEqualToString:@"hello world"]?@"true":@"false");
6.不区分大小写比较,相同返回0
NSLog(@"str cmp result is %ld",[strTest compare:@"my name is xY" options:NSCaseInsensitiveSearch]);
NSLog(@"str cmp result is %ld",[strTest caseInsensitiveCompare:@"my name is xY"]);
7.判断NSString是否以某个字符串开头
NSLog(@"str has Prefix %s",[strTest hasPrefix:@"m1"]?"true":"false");
8.判断NSString是否以某个字符串结尾
NSLog(@"str has Suffix %s",[strTest hasSuffix:@"xy"]?"true":"false");
9.字符串查找,下面输出结果为location is 3,len is 4
NSRange range=[strTest rangeOfString:@"Name" options:NSCaseInsensitiveSearch];
if (range.location==NSNotFound){
NSLog(@"not found");
}else{
NSLog(@"loaction is %ld,len is %ld",range.location,range.length);
}
10.从后往前查找
NSRange range=[strTest rangeOfString:@"Name" options:NSCaseInsensitiveSearch |
NSBackwardsSearch];
if (range.location==NSNotFound){
NSLog(@"not found");
}else{
NSLog(@"loaction is %ld,len is %ld",range.location,range.length);
}
11.指定范围查找
NSRange searchRange=NSMakeRange(0, 9);
NSRange range=[strTest rangeOfString:@"Name" options:NSCaseInsensitiveSearch |
NSBackwardsSearch range:searchRange];
if (range.location==NSNotFound){
NSLog(@"not found");
}else{
NSLog(@"loaction is %ld,len is %ld",range.location,range.length);
}
12.返回字符串的小写,不会修改原有字符串
NSLog(@"%@",[strTest lowercaseString]);
13.返回字符串的大写,不会修改原有字符串
NSLog(@"%@",[strTest uppercaseString]);
14.返回单词首字母大写的字符串,不会修改原有字符串
NSLog(@"%@",[strTest capitalizedString]);
15.返回指定位置上的字符
NSLog(@"%c",[@"123456" characterAtIndex:0]);
16.字符串转基本类型,例如int,float,double等,值得一提的是,如果字符串格式不正确不会产生异常,而是会返回0或者0.000
NSLog(@"%d",[@"122" intValue]);
NSLog(@"%f",[@"122.3" floatValue]);
NSLog(@"%f",[@"3.14154343" doubleValue]);
17.字符串截取
NSLog(@"%@",[@"123456" substringFromIndex:2]);
NSLog(@"%@",[@"123456" substringToIndex:2]);
NSLog(@"%@",[@"123456" substringWithRange:NSMakeRange(0, 2)]);
18.字符串分割转数组
NSArray *array = [@"1,2,3,4,5,6" componentsSeparatedByString:@","];
NSLog(@"%@",array);
19.字符串数组拼接为路径
NSMutableArray *components = [NSMutableArray array];
[components addObject:@"Users"];
[components addObject:@"CentralPerk"];
[components addObject:@"Desktop"];
NSString *path = [NSString pathWithComponents:components];
NSLog(@"%@",path);
20.路径分割成字符串数组
NSArray *array = [@"user/xy/music" pathComponents];
NSLog(@"%@",array);
21.判断是否为绝对路径(依据:是否以'/'开始)
NSString *path = @"/Users/CentralPerk/Desktop";
NSLog(@"%i",[path isAbsolutePath]);
22.获取路径中最后一个目录
NSLog(@"%@",[@"user/xy/music" lastPathComponent]);
23.删除路径中最后一个目录,不会影响原有路径
NSString *path=@"user/xy/music";
NSLog(@"%@,%@",[path stringByDeletingLastPathComponent],path);
24.字符串拼接
NSString *path=@"user/xy/music";
NSLog(@"%@",[path stringByAppendingPathComponent:@"111"]);
NSLog(@"%@",[path stringByAppendingString:@"222"]);
NSLog(@"%@",[path stringByAppendingFormat:@"%@%@",@"b",@"c"]);
25.文件扩展名操作,不影响原有字符串
NSString *path=@"user/xy/music/1.mp3";
//获取文件扩展名
NSLog(@"%@",[path pathExtension]);
//添加拓展名,不需要带.
NSLog(@"%@",[path stringByAppendingPathExtension:@"mp3"]);
//删除拓展名,带.一块删除
NSLog(@"%@",[path stringByDeletingPathExtension]);
26.字符串替换
NSString *path=@"user/xy/music/1.mp3";
NSLog(@"%@",[path stringByReplacingOccurrencesOfString:@"xy" withString:@"daxiongmao"]);