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 ...
随机推荐
- 运行带cocoa pods 的项目,遇到的问题是找不到文件,解决办法
打开终端,进入项目所在的目录,也就是和Podfile在同一目录下,输入以下命令(由于已经有Podfile,所以不需要再创建Podfile): pod update 过几秒(也许需要十几秒,取决于你的 ...
- 动态生成tr,并将其下控件的值拼接后传到后台并保存
有两个表(主表和子表),现在需要根据主表某一个字段动态的生成记录(一条记录就一个tr),然后再讲tr下控件的各个值取出来,传到后台,并保存到子表. html代码: <!--#for(Record ...
- 直接请求json文件爬取天眼查企业信息(未解决验证码问题)——python3实现
几个月前...省略一堆剧情...直接请求json文件爬取企业信息未成功,在知乎提问后,得到解决,有大佬说带上全部headers和cookie是可以的,我就又去试了下,果然可以(之前自己试的时候不行,没 ...
- Hibernate的面试题
1.Hibernate工作原理和为什么要用? 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Session 4.创建事务Transation 5.持 ...
- NOPI Excel插件导入导出 图片批注
dateTable导出到excel的MemoryStream /// <summary> /// DataTable导出到Excel的MemoryStream Export() /// & ...
- Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)
主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...
- java虚拟机之引用
强引用: 类似:object A=new Object();这样的引用,只要强引用还存在,垃圾回收期就永远不会回收被引用的对象,eg:这里的new Oject(). 软引用: 一些还有用,但是非必 ...
- thinkphp3.2.3在框架截取文字
Common/Common/function.php加入以下代码 /** * * 字符截取 * @param string $string * @param int $start * @param i ...
- LintCode StrStr
1. 讨论目标字符串若为空, 则返回-1: 资源字符串若为空, 则返回-1. 2.讨论目标字符串个数为零, 则返回0: 资源字符串个数为零, 则返回-1. 3. 插入旗帜来使第二循环的结束为有条件地返 ...