常见的NSString和NSMutableString方法:

NSString方法:

[plain] view
plain
copy

 
  1. +(id) stringWithContentsOfFile:path encoding:enc error:err
  2. 创建一个新字符串并将其设置为path指定的文件的内容,使用字符编码enc,如果非零,则返回err中错误
  3. +(id) stringWithContentsOfURL:url encoding:enc error:err
  4. 创建一个新的字符串,并将其设置为url的内容,使用字符编码enc,如果非零,则返回err中的错误
  5. +(id) string
  6. 创建一个新的空字符串
  7. +(id) stringWithString:nsstring
  8. 创建一个新的字符串,并将其设置为nsstring
  9. -(id)initWithString:nsstring
  10. 将分配的字符串设置为nsstring
  11. -(id) initWithContentsOfFile:path encoding:enc error:err
  12. 将字符串设置为path制定的文件的内容
  13. -(id) initWithContentsOfURL:url encoding:enc error:err
  14. 将字符串设置为url(NSURL *)url的内容,使用字符编码enc,如果非零,则返回err中的错误
  15. -(id) (UNSIgned int)length
  16. 返回字符串中的字符数目
  17. -(unichar)characterAtIndex:i
  18. 返回索引i的Unicode字符
  19. -(NSString *)substringFromIndex:i
  20. 返回从i开始知道结尾的子字符串
  21. -(NSString *)substringWithRange:range
  22. 根据指定范围返回子字符串
  23. -(NSString *)substringToIndex:i
  24. 返回从该字符串开始到索i的子字符串
  25. -(NSComparator *)caseInsensitiveCompare:nsstring
  26. 比较两个字符串,忽略大小写
  27. -(NSComparator *)compare:nsstring
  28. 比较两个字符串
  29. -(BOOL)hasPrefix:nsstring
  30. 测试字符串是否以nsstring开始
  31. -(BOOL)hasSuffix:nsstring
  32. 测试字符串是否以nsstrng结尾
  33. -(BOOL)isEqualToString:nsstring
  34. 测试两个字符串是否相等
  35. -(NSString *) capitalizedString
  36. 返回每个单词首字母大写的字符串(每个单词的其余字母转换为小写)
  37. -(NSString *)lowercaseString
  38. 返回转换为小写的字符串
  39. -(NSString *)uppercaseString
  40. 返回转换为大写的字符串
  41. -(const char*)UTF8String
  42. 返回转换为UIF-8字符串的字符串
  43. -(double)doubleValue
  44. 返回转换为double的字符串
  45. -(float)floatValue
  46. 返回转换为浮点值的字符串
  47. -(NSInteger)integerValue
  48. 返回转换为NSInteger整数的字符串
  49. -(int)intValue
  50. 返回转换为整数的字符串
  51. NSMutableString方法
  52. +(id) stringWithCapacity:size
  53. 创建一个字符串,初始包含size的字符
  54. -(id) initWithCapacity:size
  55. 使用初始容量为size的字符串来初始化字符串
  56. -(void) setString:nsstring
  57. 将字符串设置为nsstring
  58. -(void) appendString:nsstring
  59. 在接收者的末尾附加nsstring
  60. -(void) deleteCharactersInRange:range
  61. 删除指定range中的字符
  62. -(void) insertString:nsstring atIndex:i
  63. 以索引i为起始位置插入nsstring
  64. -(void) replaceCharactersInRange:range withString:nsstring
  65. 使用nsstring替换range指定的字符
  66. -(void) replaceOccurrencesOf
  67. String:nsstring withString:nsstring2 options:opts range:range

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

NSArray的基本操作代码:

[plain] view
plain
copy

 
    1. /*---------------------------创建数组------------------------------*/
    2. //NSArray *array = [NSArray alloc] initWithObjects:
    3. @"One",@"Two",@"Three",@"Four",nil];
    4. self.dataArray = array;
    5. [array release];
    6. //- (unsigned) Count;数组所包含对象个数;
    7. NSLog(@"self.dataArray cound:%d",[self.dataArray count]);
    8. //- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;
    9. NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);
    10. /*--------------------------从一个数组拷贝数据到另一数组(可变数级)----------------------------*/
    11. //arrayWithArray:
    12. //NSArray *array1 = [NSArray alloc] init];
    13. NSMutableArray *MutableArray = [NSMutableArray alloc] init];
    14. NSArray *array = [NSArray arrayWithObjects:
    15. @"a",@"b",@"c",nil];
    16. NSLog(@"array:%@",array);
    17. MutableArray = [NSMutableArray arrayWithArray:array];
    18. NSLog(@"MutableArray:%@",MutableArray);
    19. array1 = [NSArray arrayWithArray:array];
    20. NSLog(@"array1:%@",array1);
    21. //Copy
    22. //id obj;
    23. NSMutableArray *newArray = [NSMutableArray alloc] init];
    24. NSArray *oldArray = [NSArray arrayWithObjects:
    25. @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
    26. NSLog(@"oldArray:%@",oldArray);
    27. for(int i = 0; i < [oldArray count]; i++)
    28. {
    29. obj = [oldArray objectAtIndex:i] copy];
    30. [newArray addObject: obj];
    31. }
    32. //
    33. NSLog(@"newArray:%@", newArray);
    34. [newArray release];
    35. //快速枚举
    36. //NSMutableArray *newArray = [NSMutableArray alloc] init];
    37. NSArray *oldArray = [NSArray arrayWithObjects:
    38. @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
    39. NSLog(@"oldArray:%@",oldArray);
    40. for(id obj in oldArray)
    41. {
    42. [newArray addObject: obj];
    43. }
    44. //
    45. NSLog(@"newArray:%@", newArray);
    46. [newArray release];
    47. //Deep copy
    48. //NSMutableArray *newArray = [NSMutableArray alloc] init];
    49. NSArray *oldArray = [NSArray arrayWithObjects:
    50. @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
    51. NSLog(@"oldArray:%@",oldArray);
    52. newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);
    53. NSLog(@"newArray:%@", newArray);
    54. [newArray release];
    55. //Copy and sort
    56. //NSMutableArray *newArray = [NSMutableArray alloc] init];
    57. NSArray *oldArray = [NSArray arrayWithObjects:
    58. @"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];
    59. NSLog(@"oldArray:%@",oldArray);
    60. NSEnumerator *enumerator;
    61. enumerator = [oldArray objectEnumerator];
    62. id obj;
    63. while(obj = [enumerator nextObject])
    64. {
    65. [newArray addObject: obj];
    66. }
    67. [newArray sortUsingSelector:@selector(compare:)];
    68. NSLog(@"newArray:%@", newArray);
    69. [newArray release];
    70. /*---------------------------切分数组------------------------------*/
    71. //从字符串分割到数组- componentsSeparatedByString:
    72. NSString *string = [NSString alloc] initWithString:@"One,Two,Three,Four"];
    73. NSLog(@"string:%@",string);
    74. NSArray *array = [string componentsSeparatedByString:@","];
    75. NSLog(@"array:%@",array);
    76. [string release];
    77. //从数组合并元素到字符串- componentsJoinedByString:
    78. NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
    79. NSString *string = [array componentsJoinedByString:@","];
    80. NSLog(@"string:%@",string);
    81. /*******************************************************************************************
    82. NSMutableArray
    83. *******************************************************************************************/
    84. /*---------------给数组分配容量----------------*/
    85. //NSArray *array;
    86. array = [NSMutableArray arrayWithCapacity:20];
    87. /*--------------在数组末尾添加对象----------------*/
    88. //- (void) addObject: (id) anObject;
    89. //NSMutableArray *array = [NSMutableArray arrayWithObjects:
    90. @"One",@"Two",@"Three",nil];
    91. [array addObject:@"Four"];
    92. NSLog(@"array:%@",array);
    93. /*--------------删除数组中指定索引处对象----------------*/
    94. //-(void) removeObjectAtIndex: (unsigned) index;
    95. //NSMutableArray *array = [NSMutableArray arrayWithObjects:
    96. @"One",@"Two",@"Three",nil];
    97. [array removeObjectAtIndex:1];
    98. NSLog(@"array:%@",array);
    99. /*-------------数组枚举---------------*/
    100. //- (NSEnumerator *)objectEnumerator;从前向后
    101. //NSMutableArray *array = [NSMutableArray arrayWithObjects:
    102. @"One",@"Two",@"Three",nil];
    103. NSEnumerator *enumerator;
    104. enumerator = [array objectEnumerator];
    105. id thingie;
    106. while (thingie = [enumerator nextObject]) {
    107. NSLog(@"thingie:%@",thingie);
    108. }
    109. //- (NSEnumerator *)reverseObjectEnumerator;从后向前
    110. //NSMutableArray *array = [NSMutableArray arrayWithObjects:
    111. @"One",@"Two",@"Three",nil];
    112. NSEnumerator *enumerator;
    113. enumerator = [array reverseObjectEnumerator];
    114. id object;
    115. while (object = [enumerator nextObject]) {
    116. NSLog(@"object:%@",object);
    117. }
    118. //快速枚举
    119. //NSMutableArray *array = [NSMutableArray arrayWithObjects:
    120. @"One",@"Two",@"Three",nil];
    121. for(NSString *string in array)
    122. {
    123. NSLog(@"string:%@",string);
    124. }
    125. /*******************************************************************************************
    126. NSDictionary
    127. *******************************************************************************************/
    128. /*------------------------------------创建字典------------------------------------*/
    129. //- (id) initWithObjectsAndKeys;
    130. //NSDictionary *dictionary = [NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];
    131. NSString *string = [dictionary objectForKey:@"One"];
    132. NSLog(@"string:%@",string);
    133. NSLog(@"dictionary:%@",dictionary);
    134. [dictionary release];
    135. /*******************************************************************************************
    136. NSMutableDictionary
    137. *******************************************************************************************/
    138. /*------------------------------------创建可变字典------------------------------------*/
    139. //创建
    140. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    141. //添加字典
    142. [dictionary setObject:@"One" forKey:@"1"];
    143. [dictionary setObject:@"Two" forKey:@"2"];
    144. [dictionary setObject:@"Three" forKey:@"3"];
    145. [dictionary setObject:@"Four" forKey:@"4"];
    146. NSLog(@"dictionary:%@",dictionary);
    147. //删除指定的字典
    148. [dictionary removeObjectForKey:@"3"];
    149. NSLog(@"dictionary:%@",dictionary);
    150. /*******************************************************************************************
    151. NSValue(对任何对象进行包装)
    152. *******************************************************************************************/
    153. /*--------------------------------将NSRect放入NSArray中------------------------------------*/
    154. //将NSRect放入NSArray中
    155. NSMutableArray *array = [NSMutableArray alloc] init];
    156. NSValue *value;
    157. CGRect rect = CGRectMake(0, 0, 320, 480);
    158. value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];
    159. [array addObject:value];
    160. NSLog(@"array:%@",array);
    161. //从Array中提取
    162. value = [array objectAtIndex:0];
    163. [value getValue:&rect];
    164. NSLog(@"value:%@",value);
    165. /*******************************************************************************************
    166. 从目录搜索扩展名为jpg的文件
    167. *******************************************************************************************/
    168. //NSFileManager *fileManager = [NSFileManager defaultManager];
    169. NSString *home;
    170. home = @"../Users/";
    171. NSDirectoryEnumerator *direnum;
    172. direnum = [fileManager enumeratorAtPath: home];
    173. NSMutableArray *files = [NSMutableArray alloc] init];
    174. //枚举
    175. NSString *filename;
    176. while (filename = [direnum nextObject]) {
    177. if([filename pathExtension] hasSuffix:@"jpg"]){
    178. [files addObject:filename];
    179. }
    180. }
    181. //快速枚举
    182. //for(NSString *filename in direnum)
    183. //{
    184. //    if([filename pathExtension] isEqualToString:@"jpg"]){
    185. //        [files addObject:filename];
    186. //    }
    187. //}
    188. NSLog(@"files:%@",files);
    189. //枚举
    190. NSEnumerator *filenum;
    191. filenum = [files objectEnumerator];
    192. while (filename = [filenum nextObject]) {
    193. NSLog(@"filename:%@",filename);
    194. }
    195. //快速枚举
    196. //for(id object in files)
    197. //{
    198. //    NSLog(@"object:%@",object);
    199. //}

NSString和NSMutableString常用方法+NSArray常用代码 (转)的更多相关文章

  1. 字符串NSString与NSMutableString常用方法

    NSString 1.初始化 NSString *str1 = @"a OC Program"; 2.初始化 NSString *str2 = [[NSString alloc] ...

  2. [转] NSString / NSMutableString 字符串处理,常用代码

     原文 :  http://justcoding.iteye.com/blog/1405951 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString , ...

  3. 【转】 NSString / NSMutableString 字符串处理,常用代码 (实例)

    Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...

  4. NSString / NSMutableString 字符串处理,常用代码 (实例)

    http://blog.csdn.net/likendsl/article/details/7417878 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableSt ...

  5. NSString和NSMutableString的创建及其一些常用方法

    NSString和NSMutableString都是对象类型,是NSObject的子类.NSString是不可变字符串,NSMutableString是可变字符串 一.NSString的创建 1.创建 ...

  6. Foundation框架-NSString和NSMutableString

    可变与不可变的字符串 --1-- Foundation框架介绍 1.1 框架介绍 --2-- NSString 2.1 NSString介绍及使用 2.2 NSString创建方式  2.3 从文件中 ...

  7. 关于NSString和NSMutableString的相关用法和基本介绍

    Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...

  8. iOS开发常用代码块

    遍历可变数组的同时删除数组元素 NSMutableArray *copyArray = [NSMutableArray arrayWithArray:array]; NSString *str1 = ...

  9. iOS基础-NSString及NSMutableString剖析

    一.NSString头文件 NSString : NSObject 实现协议: NSCopying/NSMutableCopying/NSSecureCoding 类别: //扩展类别 NSStrin ...

随机推荐

  1. SQLite常用命令总结

    1) 创建数据库文件: >SQLite3 d:\test.db 回车 就生成了一个test.db在d盘. 这样同时也SQLite3挂上了这个test.db 2) 用.help可以看看有什么命令 ...

  2. u-boot链接脚本分析

    eclipse 64位下载地址:http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release ...

  3. Kruskal算法

    1.基本思想:设无向连通网为G=(V, E),令G的最小生成树为T=(U, TE),其初态为U=V,TE={ },然后,按照边的权值由小到大的顺序,考察G的边集E中的各条边.若被考察的边的两个顶点属于 ...

  4. tabhost使用

    Tabhost用法 使用方法一:使用同一个布局文件 在xml中如此定义tabhost: <RelativeLayout xmlns:android="http://schemas.an ...

  5. vue学习笔记

    来公司以后就一直在用vue框架,不管是业务代码,还是做vue组件.关于vue有一些点是文档中没有提及的,记录一下以便以后查询- 一.Vue的特点 新一代 Vue.js 框架非常关注如何用极少的外部特性 ...

  6. Winscp sftp远程linux服务器需要预设密码,怎么解决

    需要在root账户下修改/etc/ssh/sshd_config 文件中PermitEmptyPasswords no改成yes

  7. ftpget 从Windows FTP服务端获取文件

    /********************************************************************************* * ftpget 从Windows ...

  8. robotframework接口测试初探1

    robotframework这个框架最近很多人在使用它,大部分是和selenium结合的,大概看了下,然后发现这个做接口测试感觉也还不错,初步研究了下 环境安装: robotframework这个环境 ...

  9. PPP 转义字符 编码 和 解码

    #include <stdio.h> #include <string.h> // PPP数据帧每一帧都以标识字符0x7E开始和结束: // 由于标识字符的值是0x7E,因此当 ...

  10. ion-refresher 下拉更新数据

    使用指令ion-refresher可以为容器eg:ion-scroll 和 ion-content进行拉动刷新 <ion-scroll> <ion-refresher on-refr ...