码迷,mamicode.com
首页 > Web开发 > 详细

NSString的几个方法(rangeOfString,hasPrefix,hasSuffix,改变大小写...)

时间:2016-04-18 11:35:49      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:

- (NSRange)rangeOfString:(NSString *)searchString;//查找字符串中是包涵在某个字符串,并返回其开始位置和长度

例:

NSRange range = [link rangeOfString:@"target=_blank"];

if(range.location != NSNotFound)

{

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:link]];

}
-(BOOL) hasPrefix:(NSString *) //astring;检查字符串是否以astring开头; 
-(BOOL) hasSuffix:(NSString *) //astring;检查字符串是否以astring结尾;
 
1.) lowercaseString: 将字符串转换为小写
2.) uppercaseString: 将字符串转换为大写
3.) capitalizedString:将字符串的首字母大写
 
忽略大小写比较
//忽略大小写进行比较,返回值与compare一样  
   result = [str1 caseInsensitiveCompare:str2];  
   if (result == NSOrderedAscending) {  
       NSLog(@"右边 > 左边");  
   }else if(result == NSOrderedSame){  
       NSLog(@"内容相同");  
   }else if (result == NSOrderedDescending){  
       NSLog(@"左边 > 右边");  
   } 
//拓展名  
        //获取拓展名,不带.  
        NSString *str2 = @"Users/CentralPerk/Desktop/test.txt";  
        NSLog(@"%@",[str2 pathExtension]);  
        //添加拓展名,不需要带.  
        NSLog(@"%@",[str2 stringByAppendingPathExtension:@"mp3"]);  
        //删除拓展名,带.一块删除  
        NSLog(@"%@",[str2 stringByDeletingPathExtension]); 
        //字符串的截取  
        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的几个方法(rangeOfString,hasPrefix,hasSuffix,改变大小写...)

标签:

原文地址:http://www.cnblogs.com/wlsxmhz/p/5403396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!