//@""空的字符串对象-------分割NSString * ptr = @"I am a man";NSArray * array = [ptr componentsSeparatedByString:@" "];//将字符串整体作为分割条件 返回值为NSArray不可变数组NSMutableArray * array1 = [NSMutableArray arrayWithArray:array];//若修改,则将NSArray转化为NSMutableArrayNSArray * array2 = [ptr componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@", "]];//以字符串中的字符作为分割条件进行分割《字符集合》NSCharacterSet 这是个字符集合类[NSCharacterSet characterSetWithCharactersInString:@", "]//把字符串转化为字符集合-------拼接NSString * str = [array componentsJoinedByString:@" "];若分割条件出现在开头或者结尾,则会出现空串@"",如果不需要,则需要转化成NSMutableString对空串进行处理func1: [array1 removeObject:@""]; //找到空串直接删除func2: for(id obj in array1){ if([obj length] == 0) //空串的长度为0 if([obj isEqualToString:@""]) //与空串进行比较(字符串是不能进行==比较的,要使用函数) }可变数组:
NSMutableArray * array = [[NSMutableArray alloc]initWithObjects:@"one",@"two",@"three",@"four", nil];[array addObject:@"five"];//在数组尾部插入元素[array insertObject:@"six" atIndex:5];//在数组指定下标位置插入元素(不能越界,最大值为length)[array removeObject:@"six"];//删除指定元素(数组中出现的所有位置都将被删除)[array removeObject:@"two" inRange:NSMakeRange(0, 3)];//从指定下标位置开始的指定长度内删除指定元素[array removeLastObject];//删除最后一个元素[array removeAllObjects];//删除所有元素 [array replaceObjectAtIndex:3 withObject:@"ios"];//将指定下标位置元素替换为指定的元素[array exchangeObjectAtIndex:0 withObjectAtIndex:3];//将指定下标的两个元素进行交换不可变数组:
NSArray * array = [[NSArray alloc]initWithObjects:@"one",@"two",@"three",@"one", nil];NSUInteger index = [array indexOfObject:@"one123"];//返回第一个找到的数组成员对应的下标 找不到返回NSNotFoundindex = [array indexOfObject:@"one" inRange:NSMakeRange(1, 3)];//在指定范围内查找 if (index != NSNotFound) { NSLog(@"%ld",index); }----抽取 组成新的数组NSArray * array1 = [array objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)]];《数字集合》NSIndexSet这是个数字集合类[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)]产生一个数字集合原文地址:http://my.oschina.net/outatu/blog/124967