码迷,mamicode.com
首页 > 其他好文 > 详细

NSString的常用方法

时间:2015-05-15 17:11:15      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

大小写处理 Upper & Lower case

-(NSString *)uppercaseString     -> Alter to upper all

-(NSString *)lowercaseString      -> Alter to lower all

-(NSString *)capitalizedString     ->Initial upper, rest lower

void caseTest(){

NSString *str=@"GuangDong";

NSLog(@"Uppercase:%@",[str uppercaseString]);

NSLog(@"capitalizestring:%@",[@"age" capitalizedString]);

}


 

NSString  Cmparison 字符串比较  3

-(BOOL)isEqualToString:(NSString *)String             ->compare two string content ,if same return YES otherwise return NO

-(NSComparisonResult)compare:(NSString *)strng     ->compare two string by each letter,return object NSComparisonResult to show the result.

//NSComparisonResult  is a enum with three values

//NSOrderedDescending(left>right),NSOrderedAscending,NSOrderedSame

-(NSComparisonResult)caseInsensitiveCompare:(NSString *)string

//Ignore upper or lower case, return same to compare

//敲这么多英文感觉自己好装B啊,哈哈哈

void compare(){

BOOL result=[@"abc" isEqualToString:@"Abc"];// return NO(YES =1,NO=0)

NSLog(@‘%i‘,result);

NSComparisonResult result2=[@"abc" compare@"ABC"];

if(result2==NSOrderedSame){

NSLog(@"same");

}else if(result2==NSOrderedAscending){

NSLog(@"left<right");

}

else if(result2==NSOrderedDescending){

NSLog(@"left>right");

}

}

 //Same lower case letter is larger than upper case of 32

 


 NSString search certain  string. NSString 中搜索字符串

-(BOOL)hasPrefix:(NSString *)astring     //Whether start with astring

-(BOOL)hasSuffix:(NSString *)astring     //Whether end with a string

-(NSRange)rangeOfString:(NSString *)astring

//Check whether include astring ,if include,return the position of astring,if not,return location as -1 of NSRange,length is 0

-(NSRange)rangeOfString:(NSString *)astring options:(NSStringCompareOptions):mask range:(NSString)

void search(){

NSString *str=@"123456456.txt";//only return the first

NSLog(@"Start with 12:%i"[str hasPrefix:@"12"];

NSLog(@"End with txt:%i"[str hasSuffix:@"txt"];

 

NSRange range=[str rangeOfString:@"345"];

NSLog(@"Include :%@",NSStringFromRange(range)];

if(range.location==NSNNotFound){

NSLog(@"Can‘t find");//range.length==0;

}else{

NSLog(@"%@",NSStringFromRange(range));

}

//Search from back

range=[str rangeOfString:@"456" options:NSBackwardSearch];

NSLog(@"%@",NSStringFromRange(range));

//assign your area of search

range=[str rangeOfString:@"456" options:NSBackwardSearch range];

NSLog(@"%@",NSStringFromRange(range));

}

NSString的常用方法

标签:

原文地址:http://www.cnblogs.com/yesihoang/p/4506117.html

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