oc-Foundation框架-NSString-常用方法

NSString *str=@"ChengDu";
    str=[str uppercaseString];//变大写
    NSLog(@"%@",str);
   str=[str lowercaseString];//变小写
    NSLog(@"%@",str);
    str=[str capitalizedString];//首字母大写
     NSLog(@"%@",str);

 

 

[@"abc1" isEqualToString:@"abc"]//比较内容是否相等,地址也比较就直接用“==”

 NSComparisonResult *result = [@"abc1" compare:@"abc"];//caseInsensitiveCompare忽略大小写进行比较
if(result == NSOrderedSame){
          NSLog(@"相等");
      }else if (result==NSOrderedAscending){
          NSLog(@"升序");
      }else if(result==NSOrderedDescending){
          NSLog(@"降序");
      }
返回结果是个枚举。一看就指定什么意思,ns框架,ordered排序,asc,desc和数据库里面的升降一样。

 

NSString *str=@"123456.txt";
    int i=[str hasPrefix:@"12"]; //是否用什么开头
    int b=[str hasSuffix:@".txt"];//是否用什么结尾
    NSLog(@"%d,%d",i,b);

 

NSString *str=@"123456.txt";
   NSRange range= [str rangeOfString:@"345"];
   NSLog(@"%@",NSStringFromRange(range));//返回{2,3}
   NSRange rr=NSMakeRange(0, 5);
   NSString *str=@"12345634567.txt";
   NSRange range= [str rangeOfString:@"345"];
   NSRange range1=[str rangeOfString:@"345" options:NSBackwardsSearch];//反向查找
   NSRange range2=[str rangeOfString:@"345" options:NSBackwardsSearch range:rr];//反向从一个范围查找
   NSLog(@"%@",NSStringFromRange(range2));

 

 

 str=[str substringFromIndex:3];
    str=[str substringToIndex:3];
    str=[str substringWithRange:NSMakeRange(2, 3)];
//字符串的截取一看就知道是什么意思了

 

 NSString *str=@"12,345,6.t,xt";
    NSArray *arry=[str componentsSeparatedByString:@","];
    for(NSString *str in arry){
        NSLog(@"%@",str);
    }//componentsSeparatedByString字符串的分割

 

posted @ 2014-03-20 21:28  离子  阅读(239)  评论(0编辑  收藏  举报