字符串类 NSString、NSMutableString
创建字符串:
NSString*heigth=[NSString stringWithFormat:@"Youheigthis%dfeet,%dinches",5,11];
返回字符串中的字符的个数:
unsignedintlength=[heigth length];
返回Bool值的字符串比较函数:
-(BOOL)isEqualToString:(NSString*)aString;//比较两个字符串的内容是否相等
还可以使用compare:方法
-(NSComparisonResult)compare :(NSString*)string;//逐个字符比较
检查字符串是否以另一个字符串开头
-(BOOL)hasPrefix:(NSString*)aString;
判断字符串是否是以另一个字符串结尾
-(BOOL)hasSuffix:(NSString*)aString;
如果你想知道字符串内的某处是否包含其他字符串,使用rangeOfString: 方法
-(NSRange)rangeOfString :(NSString*)aString;
NSString是不可变的,NSMutableString是可变的。用方法stringWithCapacity :来创建
NSMutableString*string=[NSMutableStringstringWithCapacity :42];
可以使用appendString :或appendFormat: 来附加新字符串:
-(void)appendString:(NSString*)aString;
-(void)appendFormat:(NSString*)format,...;
可以使用deleteCharactersInRange: 方法删除字符串中的字符
-(void)deleteCharactersInRange: (NSRange)range;
集合类 NSArray 用来存储对象的有序列表(任意类型的对象)
限制:只能存储Objective—C的对象,不能存储C语言的基本数据类型(int、float、enum、struct、或者NSArray
中的随机指针)。同时也不能存储nil(对象的零值或NULL值)
/*---------------------------创建数组------------------------------*/ NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil]; self.dataArray = array; [array release]; //- (unsigned) Count;数组所包含对象个数; NSLog(@"self.dataArray cound:%d",[self.dataArray count]); //- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象; NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);
//创建一个新的NSArray NSArray*array=[NSArray arrayWithObjects:@"one",@"two",nil]; //获得包含的对象个数 - (unsigned)count; //获得特定索引处的对象 - (id)objectAtIndex:(unsigned int)index ;NSArray是不可变数组,数组中包含的对象是可以改变的,但是数组对象本身是不会改变的。
/*--------------------------从一个数组拷贝数据到另一数组(可变数级)----------------------------*/ //arrayWithArray: //NSArray *array1 = [NSArray alloc] init]; NSMutableArray *MutableArray = [NSMutableArray alloc] init]; NSArray *array = [NSArray arrayWithObjects: @"a",@"b",@"c",nil]; NSLog(@"array:%@",array); MutableArray = [NSMutableArray arrayWithArray:array]; NSLog(@"MutableArray:%@",MutableArray); array1 = [NSArray arrayWithArray:array]; NSLog(@"array1:%@",array1);
//Copy //id obj; NSMutableArray *newArray = [NSMutableArray alloc] init]; NSArray *oldArray = [NSArray arrayWithObjects: @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil]; NSLog(@"oldArray:%@",oldArray); for(int i = 0; i < [oldArray count]; i++) { obj = [oldArray objectAtIndex:i] copy]; [newArray addObject: obj]; } // NSLog(@"newArray:%@", newArray); [newArray release];
//快速枚举 //NSMutableArray *newArray = [NSMutableArray alloc] init]; NSArray *oldArray = [NSArray arrayWithObjects: @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil]; NSLog(@"oldArray:%@",oldArray); for(id obj in oldArray) { [newArray addObject: obj]; } // NSLog(@"newArray:%@", newArray); [newArray release];
//Copy and sort //NSMutableArray *newArray = [NSMutableArray alloc] init]; NSArray *oldArray = [NSArray arrayWithObjects: @"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil]; NSLog(@"oldArray:%@",oldArray); NSEnumerator *enumerator; enumerator = [oldArray objectEnumerator]; id obj; while(obj = [enumerator nextObject]) { [newArray addObject: obj]; } [newArray sortUsingSelector:@selector(compare:)]; NSLog(@"newArray:%@", newArray); [newArray release];