A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 332439409 中级黑马   /  2016-3-19 21:27  /  1226 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

NSString的几条小技巧
创建字符串常量字符串
NSString *string = @"i am an iOSDevTip!";常用创建方法
NSString *string = [[NSString alloc] init];string = @"i am an iOSDevTip too!";用initWithString创建字符串
NSString *string = [[NSString alloc] initWithString:@"iOSDevTip is here!"];格式化创建字符串int格式化字符串
int age = 20;NSString *personAge = [NSString stringWithFormat:@"this person age is %d",age];既然int格式化字符串,那么float、double等,也可以格式化字符串。
NSString格式化字符串
NSString *name = @"iOSDevTip";NSString *personName = [NSString stringWithFormat:@"this person name is %@",name];字符串比较isEqualToString方法比较
//比较字符串NSString *stingOne = @"This is an iOSDevTip!";NSString *stringTwo = @"This is an iOSDevTip!";BOOL result = [stingOne isEqualToString:stringTwo];compare方法比较
BOOL result = [stingOne compare:stringTwo] == NSOrderedSame;compare:方法返回值类型为NSComparisonResult。而NSComparisonResult有下面几个枚举值。
typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};字符串大小写转换小写转大写
NSString *string = @"This is an iOSDevTip!";[string lowercaseString];大写转小写
NSString *string = @"This is an iOSDevTip!";[string uppercaseString];截取字符串substringToIndex截取字符串
NSString *string = @"This is a operation string!";NSString *subToString = [string substringToIndex:6];截取的subToString为This i
substringFromIndex截取字符串
NSString *subFromString = [string substringFromIndex:6];截取的subFromString为s a operation string!
substringWithRange截取字符串
NSString *rangeString = [string substringWithRange:NSMakeRange(6, 3)];截取的rangeString为s a!
判断字符串是否包含另一个字符串rangeOfString判断
NSString *string1 = @"This is a iOSDevTip";NSString *string2 = @"iOSDevTip";NSRange range = [string1 rangeOfString:string2];NSInteger location = range.location;NSInteger leight = range.length;NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%ld,Leight:%ld",location,leight]];NSLog(@"logString:%@",logString);打印出来:
iOSStrongDemo[8837:2221170] logString:Location:10,Leight:9如果Leight为0,说明不包含。还有更多关于NSString的用法,大家一起探索吧。

2 个回复

倒序浏览
                      学习了
回复 使用道具 举报
  老师还没讲 呢  学习了。
回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马