Objective-C集合 (一)——NSString
?
?
Cocoa由两个框架组成:FoundationKit和ApplicationKit。ApplicationKit:存放了所有的用户接口对象和高级类FoundationKit:有许多实用的面向数据的低级类和数据类型。此框架存放在:/Developer/ADC?Reference?Libraty/documentaion/index.html中1.示例代码:import <Foundation/Foundation.h>int main(int argc,const char *argv[]){ NSAutorealeasePool * pool =[[NSAutorealeasePool alloc] init]; //insert your code here NSLog(@"Hello ,World"); [pool drain];//释放内存 return 0;}?2.字符串(1)创建字符串
/*备注:a.+:代表此方法属于类方法,属于类对象(而不是属于类的实例对象)并且通常用于创建新的实例,也称工厂方法。根据传递的参数创建新对象类方法也可以访问全局数据,比如AppKit中NSColor类有一些以不同颜色命名的类方法,redColor和blueColor,可以这样写 NSColor *haveBlueColor = [NSColor blueColor]; b. 省略号:表示接收多个以逗号隔开的其他参数*/+ (id)stringWithFormat: (NSString *) format,... ;NSString *height;height = [NSString stringWithFormat: @"Your height is %d feet, %d inches", 5, 11];?(2)字符串长度
- (unsigned int) length;unsigned int length = [height length];?(3)比较字符串:
- (Bool) isEqualToString: (NSString *) aString;?(4)是否区分大小写的比较
/*备注:options是位掩码,可以使用位运算符(|)来添加选项标记常用选项:NSCaseInsensitiveSearch :不区分大小写字符NSLiteralSearch:区分大小写比较NSNumericSearch:比较字符串的个数而不是字符值。*/- (NSComparisonResult) compare: (NSString *) string options:(unsigned)mask?例子:比较字符串,忽略大小写但按字符个数的多少正确排序
if([thing1 compare thing2 options: NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame){ NSLog(@"They match");}?(5)字符串是否包含别的字符串
//以另一个字符串开头- (Bool) hasPrefix: (NSString *) aString;//以另一个字符串结尾- (Bool) hasSuffix: (NSString *) aString;//是否包含其他字符- (NSRange) rangeOfString: (NSString *) aString;?(6)可变性类似于StringBuffer?,Cocoa提供了NSString的子类,叫NSMutableString.