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 ...
随机推荐
- MVC之路随记1--Filter的应用
功能:MVC提供过滤器Filter,使开发者不用复杂的实现AOP而直接用Filter实现同样的功能. 实现:1.定义一个类实现ActionFilterAttribute,重载借口中的方法后在Contr ...
- Cocos2dx对精灵的优化
cocos2dx针对游戏设计的不同方面会有不同的优化方案,可以对声音,对内存,对图片格式,对色彩等等进行优化.有关这些方面的方法请大家查找其他的文章.我今天要说的是如何对精灵进行优化,程序中我们用到的 ...
- jQuery学习总结
1:jQuery是什么 jQuery是继prototype之后又一个优秀的Javascript框架.它是轻量级的js库,兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, O ...
- 11——在operator=中处理自我赋值
在operator=函数中加一个测试: if(&rhs==this) copy and swap
- 麦克斯韦方程组 (Maxwell's equation)的简单解释
[转载请注明出处]http://www.cnblogs.com/mashiqi 2016/12/12 以下会用高中的物理知识和大学微积分的数学知识对麦克斯韦方程组进行一个简单的解释.希望大家都能看得懂 ...
- android 判断是否设置了锁屏密码
方式1:在小米note手机上测试,只能判断是否设置了图形解锁. android.provider.Settings.System.getInt(getContentResolver(), androi ...
- XAMPP 的安装配置(Linux 版)
--姜庭华 msn: jaimejth@live.cn --博客:http://blog.csdn.net/jaimejth 软件下载在以下网站 http://www.apachefriends.o ...
- 详解SVN 的使用
一.什么是SVN SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS. 二.SVN的下载安装 下载地址:http ...
- 《Android深度探索HAL与驱动开发》第三章阅读心得
Git是Linux内核代码对源代码进行管理的软件,他的各方面要优与其他同类的源代码管理软件. 安装Git后,查看Git文档在Linux下可以直接使用man命令看指令的帮助文档.安装git-doc后会安 ...
- 【Cocos2d-x 3.x】 场景切换生命周期、背景音乐播放和场景切换原理与源码分析
大部分游戏里有很多个场景,场景之间需要切换,有时候切换的时候会进行背景音乐的播放和停止,因此对这块内容进行了总结. 场景切换生命周期 场景切换用到的函数: bool Setting::init() { ...