您的位置:首页 > 编程语言

NSString和NSMutableString常用方法+NSArray常用代码

2012-09-05 13:54 465 查看
原址:http://www.cocoachina.com/newbie/tutorial/2010/1013/2173.html

常见的NSString和NSMutableString方法:

NSString方法:

[plain] view
plaincopyprint?

+(id) stringWithContentsOfFile:path encoding:enc error:err

创建一个新字符串并将其设置为path指定的文件的内容,使用字符编码enc,如果非零,则返回err中错误

+(id) stringWithContentsOfURL:url encoding:enc error:err

创建一个新的字符串,并将其设置为url的内容,使用字符编码enc,如果非零,则返回err中的错误

+(id) string

创建一个新的空字符串

+(id) stringWithString:nsstring

创建一个新的字符串,并将其设置为nsstring

-(id)initWithString:nsstring

将分配的字符串设置为nsstring

-(id) initWithContentsOfFile:path encoding:enc error:err

将字符串设置为path制定的文件的内容

-(id) initWithContentsOfURL:url encoding:enc error:err

将字符串设置为url(NSURL *)url的内容,使用字符编码enc,如果非零,则返回err中的错误

-(id) (UNSIgned int)length

返回字符串中的字符数目

-(unichar)characterAtIndex:i

返回索引i的Unicode字符

-(NSString *)substringFromIndex:i

返回从i开始知道结尾的子字符串

-(NSString *)substringWithRange:range

根据指定范围返回子字符串

-(NSString *)substringToIndex:i

返回从该字符串开始到索i的子字符串

-(NSComparator *)caseInsensitiveCompare:nsstring

比较两个字符串,忽略大小写

-(NSComparator *)compare:nsstring

比较两个字符串

-(BOOL)hasPrefix:nsstring

测试字符串是否以nsstring开始

-(BOOL)hasSuffix:nsstring

测试字符串是否以nsstrng结尾

-(BOOL)isEqualToString:nsstring

测试两个字符串是否相等

-(NSString *) capitalizedString

返回每个单词首字母大写的字符串(每个单词的其余字母转换为小写)

-(NSString *)lowercaseString

返回转换为小写的字符串

-(NSString *)uppercaseString

返回转换为大写的字符串

-(const char*)UTF8String

返回转换为UIF-8字符串的字符串

-(double)doubleValue

返回转换为double的字符串

-(float)floatValue

返回转换为浮点值的字符串

-(NSInteger)integerValue

返回转换为NSInteger整数的字符串

-(int)intValue

返回转换为整数的字符串

NSMutableString方法

+(id) stringWithCapacity:size

创建一个字符串,初始包含size的字符

-(id) initWithCapacity:size

使用初始容量为size的字符串来初始化字符串

-(void) setString:nsstring

将字符串设置为nsstring

-(void) appendString:nsstring

在接收者的末尾附加nsstring

-(void) deleteCharactersInRange:range

删除指定range中的字符

-(void) insertString:nsstring atIndex:i

以索引i为起始位置插入nsstring

-(void) replaceCharactersInRange:range withString:nsstring

使用nsstring替换range指定的字符

-(void) replaceOccurrencesOf

String:nsstring withString:nsstring2 options:opts range:range

根据选项opts。使用指定range中的nsstring2替换所有的nsstring。选项可以包括NSBackwardsSearch(从范围的结尾 开始搜索)NSAnchoredSearch(nsstring必须匹配范围的开始),NSLiteralSearch(执行逐字节比较以及 NSCaceInsensitiveSearch的按位或组合)

NSArray的基本操作代码:

[plain] view
plaincopyprint?

/*---------------------------创建数组------------------------------*/

//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]);

/*--------------------------从一个数组拷贝数据到另一数组(可变数级)----------------------------*/

//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];

//Deep copy

//NSMutableArray *newArray = [NSMutableArray alloc] init];

NSArray *oldArray = [NSArray arrayWithObjects:

@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];

NSLog(@"oldArray:%@",oldArray);

newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);

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];

/*---------------------------切分数组------------------------------*/

//从字符串分割到数组- componentsSeparatedByString:

NSString *string = [NSString alloc] initWithString:@"One,Two,Three,Four"];

NSLog(@"string:%@",string);

NSArray *array = [string componentsSeparatedByString:@","];

NSLog(@"array:%@",array);

[string release];

//从数组合并元素到字符串- componentsJoinedByString:

NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];

NSString *string = [array componentsJoinedByString:@","];

NSLog(@"string:%@",string);

/*******************************************************************************************

NSMutableArray

*******************************************************************************************/

/*---------------给数组分配容量----------------*/

//NSArray *array;

array = [NSMutableArray arrayWithCapacity:20];

/*--------------在数组末尾添加对象----------------*/

//- (void) addObject: (id) anObject;

//NSMutableArray *array = [NSMutableArray arrayWithObjects:

@"One",@"Two",@"Three",nil];

[array addObject:@"Four"];

NSLog(@"array:%@",array);

/*--------------删除数组中指定索引处对象----------------*/

//-(void) removeObjectAtIndex: (unsigned) index;

//NSMutableArray *array = [NSMutableArray arrayWithObjects:

@"One",@"Two",@"Three",nil];

[array removeObjectAtIndex:1];

NSLog(@"array:%@",array);

/*-------------数组枚举---------------*/

//- (NSEnumerator *)objectEnumerator;从前向后

//NSMutableArray *array = [NSMutableArray arrayWithObjects:

@"One",@"Two",@"Three",nil];

NSEnumerator *enumerator;

enumerator = [array objectEnumerator];

id thingie;

while (thingie = [enumerator nextObject]) {

NSLog(@"thingie:%@",thingie);

}

//- (NSEnumerator *)reverseObjectEnumerator;从后向前

//NSMutableArray *array = [NSMutableArray arrayWithObjects:

@"One",@"Two",@"Three",nil];

NSEnumerator *enumerator;

enumerator = [array reverseObjectEnumerator];

id object;

while (object = [enumerator nextObject]) {

NSLog(@"object:%@",object);

}

//快速枚举

//NSMutableArray *array = [NSMutableArray arrayWithObjects:

@"One",@"Two",@"Three",nil];

for(NSString *string in array)

{

NSLog(@"string:%@",string);

}

/*******************************************************************************************

NSDictionary

*******************************************************************************************/

/*------------------------------------创建字典------------------------------------*/

//- (id) initWithObjectsAndKeys;

//NSDictionary *dictionary = [NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];

NSString *string = [dictionary objectForKey:@"One"];

NSLog(@"string:%@",string);

NSLog(@"dictionary:%@",dictionary);

[dictionary release];

/*******************************************************************************************

NSMutableDictionary

*******************************************************************************************/

/*------------------------------------创建可变字典------------------------------------*/

//创建

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

//添加字典

[dictionary setObject:@"One" forKey:@"1"];

[dictionary setObject:@"Two" forKey:@"2"];

[dictionary setObject:@"Three" forKey:@"3"];

[dictionary setObject:@"Four" forKey:@"4"];

NSLog(@"dictionary:%@",dictionary);

//删除指定的字典

[dictionary removeObjectForKey:@"3"];

NSLog(@"dictionary:%@",dictionary);

/*******************************************************************************************

NSValue(对任何对象进行包装)

*******************************************************************************************/

/*--------------------------------将NSRect放入NSArray中------------------------------------*/

//将NSRect放入NSArray中

NSMutableArray *array = [NSMutableArray alloc] init];

NSValue *value;

CGRect rect = CGRectMake(0, 0, 320, 480);

value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];

[array addObject:value];

NSLog(@"array:%@",array);

//从Array中提取

value = [array objectAtIndex:0];

[value getValue:&rect];

NSLog(@"value:%@",value);

/*******************************************************************************************

从目录搜索扩展名为jpg的文件

*******************************************************************************************/

//NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *home;

home = @"../Users/";

NSDirectoryEnumerator *direnum;

direnum = [fileManager enumeratorAtPath: home];

NSMutableArray *files = [NSMutableArray alloc] init];

//枚举

NSString *filename;

while (filename = [direnum nextObject]) {

if([filename pathExtension] hasSuffix:@"jpg"]){

[files addObject:filename];

}

}

//快速枚举

//for(NSString *filename in direnum)

//{

// if([filename pathExtension] isEqualToString:@"jpg"]){

// [files addObject:filename];

// }

//}

NSLog(@"files:%@",files);

//枚举

NSEnumerator *filenum;

filenum = [files objectEnumerator];

while (filename = [filenum nextObject]) {

NSLog(@"filename:%@",filename);

}

//快速枚举

//for(id object in files)

//{

// NSLog(@"object:%@",object);

//} <span style="font-family:'Lucida Grande', Geneva, Arial, Verdana, Tahoma, Arial, 微软雅黑, 黑体, 宋体;"> </span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: