NSString和NSMutableString常用方法+NSArray常用代码 (转)
常见的NSString和NSMutableString方法:
NSString方法:
- +(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的基本操作代码:
- /*---------------------------创建数组------------------------------*/
- //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);
- //}
NSString和NSMutableString常用方法+NSArray常用代码 (转)的更多相关文章
- 字符串NSString与NSMutableString常用方法
NSString 1.初始化 NSString *str1 = @"a OC Program"; 2.初始化 NSString *str2 = [[NSString alloc] ...
- [转] NSString / NSMutableString 字符串处理,常用代码
原文 : http://justcoding.iteye.com/blog/1405951 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString , ...
- 【转】 NSString / NSMutableString 字符串处理,常用代码 (实例)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
- NSString / NSMutableString 字符串处理,常用代码 (实例)
http://blog.csdn.net/likendsl/article/details/7417878 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableSt ...
- NSString和NSMutableString的创建及其一些常用方法
NSString和NSMutableString都是对象类型,是NSObject的子类.NSString是不可变字符串,NSMutableString是可变字符串 一.NSString的创建 1.创建 ...
- Foundation框架-NSString和NSMutableString
可变与不可变的字符串 --1-- Foundation框架介绍 1.1 框架介绍 --2-- NSString 2.1 NSString介绍及使用 2.2 NSString创建方式 2.3 从文件中 ...
- 关于NSString和NSMutableString的相关用法和基本介绍
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
- iOS开发常用代码块
遍历可变数组的同时删除数组元素 NSMutableArray *copyArray = [NSMutableArray arrayWithArray:array]; NSString *str1 = ...
- iOS基础-NSString及NSMutableString剖析
一.NSString头文件 NSString : NSObject 实现协议: NSCopying/NSMutableCopying/NSSecureCoding 类别: //扩展类别 NSStrin ...
随机推荐
- android view : window
既然是view,为什么要说window,实际上着是一个很有用的东西,在展现view和设计界面上很有用,就比如说悬浮窗 但是这时候又要分清楚一个概念,window到底是什么?在activity中说过了我 ...
- vfp 智能感知拓展应用
*======================================================================================== * * Versio ...
- 推荐一些不错的计算机书籍(php c mysql linux等等)
推荐一些不错的计算机书籍. # PHP<PHP程序设计>(第2版) --PHP语法和入门最好的书<PHP5权威编程> --PHP入门后升级书<深入PHP:面向对象.模 ...
- cnblogs开篇留念
之前看过很多大牛程序员们介绍的一些经验之类的文章,几乎每个人都提到了一点就是平时要写博客,记录一些自己平时学习和工作过程中学习到的一些技术点和心得.之前也用过一些其他的网站博客,上周有同事推荐了一篇文 ...
- MySQL分布式集群之MyCAT(转)
原文地址:http://blog.itpub.net/29510932/viewspace-1664499/ 隔了好久,才想起来更新博客,最近倒腾的数据库从Oracle换成了MySQL,研究了一段时间 ...
- CEPH经常出现slow request的排查解决
现象: 通过ceph -w日志经常发现有request blocked的问题(如果虚拟机系统跑在ceph上时,就会发现严重的卡顿现象) 排查: 1.通过dstat未发现有明显的瓶颈 (dstat -t ...
- android摇一摇实现(仿微信)
这个demo模仿的是微信的摇一摇,是一个完整的demo,下载地址在最下面.下面是demo截图: 步驟: 1.手机摇动监听,首先要实现传感器接口SensorEventLi ...
- Hive自定义函数的学习笔记(1)
前言: hive本身提供了丰富的函数集, 有普通函数(求平方sqrt), 聚合函数(求和sum), 以及表生成函数(explode, json_tuple)等等. 但不是所有的业务需求都能涉及和覆盖到 ...
- 测试cookie的读写
js文件使用utf8编码 <p><img id="img" onclick="javascript:var s=document.createEleme ...
- metagenome 简介
宏基因组 ( Metagenome)(也称微生物环境基因组 Microbial Environmental Genome, 或元基因组) .是由 Handelsman 等 1998 年提出的新名词, ...