字符串
创建字符串
+ (id) stringWithFormat: (NSString *) format,...;//此处stringWithFormat:是一个工厂方法
NSString *height ; height= [NSString stringWithFormat:@"Your height is %d feet,%d feet, %d inches",5,11];
字符串比较
- (BOOL) osEqualToString:(NSString *)aString; //实例方法 比较两个字符串内容是否相同
NSString *thing1 = @"hello 5"; NSString *thing2=[NSString stringWithFormat: @"hello %d",5]; if([thing1 isEqualToString: thing2]) { NSLog(@"They are the same!"); }
- (NSComparisonResult) compare: (NSString *) aString;
//compare:将接收对象和传递过来的字符串逐个进行比较(区分大小写),显示孰大孰小
//其返回类型为一个enum型枚举,如下所示
enum{
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
};
typedef NSInteger NSComparisonResult;
不区分大小写的比较
- (NSComparisonResult) compare: (NSString *) aString options: (NSStringCompareOptions) mask;
//其中常用的mask选项如下:
NSCaseInsensitiveSearch:不区分大小写字符
NSLiteralSearch:进行完全比较,区分大小写
NSNumericSearch:比较字符串的字符个数
字符串内是否包含别的字符串
- (BOOL) hasPrefix: (NSString *) aString;
- (BOOL) hasSuffix: (NSString *) aString;
- (BOOL) rangOfString: (NSString *) aString;
可变性
+ (id) stringWithCapacity: (NSUInteger) capacity;
- (void) appendString: (NSString *) aString;
- (void) appendFormat: (NSString *) format, ...;
- (void) deleteCharactersInRange: (NSRange) aRange;