- - 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));
- [cpp] view plaincopy
- - <pre name="code" class="cpp">//字符串的截取
- - 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);
- - </pre><br><br>
复制代码
|
|