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 ...
随机推荐
- Python学习路程day16
Python之路,Day14 - It's time for Django 本节内容 Django流程介绍 Django url Django view Django models Django te ...
- javascript获取asp.net服务器端控件的值
代码如下: <%@ Page Language="C#" CodeFile="A.aspx.cs" Inherits="OrderManage_ ...
- windows下Python shell代码自动补全
Unix下实现如题功能用下面的代码: import rlcompleter, readline readline.parse_and_bind('tab: complete') 但readline不能 ...
- SQL排序
- 认识Java
java出生地:SUN Microsystems Inc<开源> -SUN : Standford University Network java之父:James Gosling ...
- 【转】JavaScript之web通信
原文转自:http://cloudbbs.org/forum.php?mod=viewthread&tid=28773&page=1&extra=#pid180304 一.前言 ...
- php组合
为了提高代码的复用性,降低代码的耦合(组合实现的两种方式) 模式一: <?php //组合模式一 class Person{ public function eat(){ echo " ...
- SILVERLIGHT 应急卫生模拟演练项目之childwindow
项目中经常要用到childwindow 默认SL提供的界面很不好看 也很难适应系统里的要求 单调的界面 木关系 可以我们可以通过BLEND自定义成我们想要的 首先新建立一个SILVERLIGHT 子窗 ...
- Android使用ListView应该注意的地方
在ListView中设置Selector为null会报空指针? mListView.setSelector(null);//空指针 试试下面这种: mListView.setSelector(new ...
- genymotion模拟器访问本地服务器
测试环境为:Ubuntu + android studio + genymotion + wifi 我用模拟器访问电脑上的服务器,需要使用的IP为10.0.3.2,其他的什么10.0.2.2和10.0 ...