• NSStirng、NSArray、以及枚举(Method小集合)


    Object-c代码  

    1. /*******************************************************************************************  
    2.  NSString  
    3.  *******************************************************************************************/  
    4. //一、NSString      
    5. /*----------------创建字符串的方法----------------*/  
    6.   
    7. //1、创建常量字符串。  
    8. NSString *astring = @"This is a String!";  
    9.   
    10.   
    11. //2、创建空字符串,给予赋值。  
    12.   
    13. NSString *astring = [[NSString alloc] init];  
    14. astring = @"This is a String!";  
    15. [astring release];  
    16. NSLog(@"astring:%@",astring);  
    17.   
    18.   
    19.   
    20. //3、在以上方法中,提升速度:initWithString方法  
    21.   
    22. NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];  
    23. NSLog(@"astring:%@",astring);  
    24. [astring release];  
    25.   
    26.   
    27.   
    28. //4、用标准c创建字符串:initWithCString方法  
    29.   
    30. char *Cstring = "This is a String!";  
    31. NSString *astring = [[NSString alloc] initWithCString:Cstring];  
    32. NSLog(@"astring:%@",astring);  
    33. [astring release];  
    34.   
    35.   
    36.   
    37. //5、创建格式化字符串:占位符(由一个%加一个字符组成)  
    38.   
    39. int i = 1;  
    40. int j = 2;  
    41. NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];  
    42. NSLog(@"astring:%@",astring);  
    43. [astring release];  
    44.   
    45.   
    46.   
    47. //6、创建临时字符串  
    48.   
    49. NSString *astring;  
    50. astring = [NSString stringWithCString:"This is a temporary string"];  
    51. NSLog(@"astring:%@",astring);  
    52.   
    53.   
    54.   
    55.   
    56. /*----------------从文件读取字符串:initWithContentsOfFile方法----------------*/      
    57.   
    58. NSString *path = @"astring.text";  
    59. NSString *astring = [[NSString alloc] initWithContentsOfFile:path];  
    60. NSLog(@"astring:%@",astring);  
    61. [astring release];  
    62.   
    63.   
    64. /*----------------写字符串到文件:writeToFile方法----------------*/      
    65.   
    66.   
    67. NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];  
    68. NSLog(@"astring:%@",astring);  
    69. NSString *path = @"astring.text";      
    70. [astring writeToFile: path atomically: YES];  
    71. [astring release];      
    72.   
    73.   
    74.   
    75.   
    76. /*----------------比较两个字符串----------------*/          
    77.   
    78. //用C比较:strcmp函数  
    79.   
    80. char string1[] = "string!";  
    81. char string2[] = "string!";  
    82. if(strcmp(string1, string2) = = 0)  
    83. {  
    84.     NSLog(@"1");  
    85. }  
    86.   
    87.   
    88.   
    89. //isEqualToString方法      
    90. NSString *astring01 = @"This is a String!";  
    91. NSString *astring02 = @"This is a String!";  
    92. BOOL result = [astring01 isEqualToString:astring02];  
    93. NSLog(@"result:%d",result);  
    94.   
    95.   
    96.   
    97.   
    98. //compare方法(comparer返回的三种值)      
    99. NSString *astring01 = @"This is a String!";  
    100. NSString *astring02 = @"This is a String!";      
    101. BOOL result = [astring01 compare:astring02] = = NSOrderedSame;      
    102. NSLog(@"result:%d",result);      
    103. //NSOrderedSame判断两者内容是否相同  
    104.   
    105.   
    106.   
    107.   
    108. NSString *astring01 = @"This is a String!";  
    109. NSString *astring02 = @"this is a String!";  
    110. BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;      
    111. NSLog(@"result:%d",result);  
    112. //NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)  
    113.   
    114.   
    115.   
    116. NSString *astring01 = @"this is a String!";  
    117. NSString *astring02 = @"This is a String!";  
    118. BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;      
    119. NSLog(@"result:%d",result);       
    120. //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)  
    121.   
    122.   
    123.   
    124. //不考虑大小写比较字符串1  
    125. NSString *astring01 = @"this is a String!";  
    126. NSString *astring02 = @"This is a String!";  
    127. BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;      
    128. NSLog(@"result:%d",result);       
    129. //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)  
    130.   
    131.   
    132.   
    133. //不考虑大小写比较字符串2  
    134. NSString *astring01 = @"this is a String!";  
    135. NSString *astring02 = @"This is a String!";  
    136. BOOL result = [astring01 compare:astring02  
    137.                         options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;      
    138. NSLog(@"result:%d",result);       
    139.   
    140. //NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。  
    141.   
    142.   
    143. /*----------------改变字符串的大小写----------------*/      
    144.   
    145. NSString *string1 = @"A String";   
    146. NSString *string2 = @"String";   
    147. NSLog(@"string1:%@",[string1 uppercaseString]);//大写  
    148. NSLog(@"string2:%@",[string2 lowercaseString]);//小写  
    149. NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小  
    150.   
    151.   
    152. /*----------------在串中搜索子串----------------*/          
    153.   
    154. NSString *string1 = @"This is a string";  
    155. NSString *string2 = @"string";  
    156. NSRange range = [string1 rangeOfString:string2];  
    157. int location = range.location;  
    158. int leight = range.length;  
    159. NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];  
    160. NSLog(@"astring:%@",astring);  
    161. [astring release];  
    162.   
    163.   
    164. /*----------------抽取子串 ----------------*/          
    165.   
    166. //-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符  
    167. NSString *string1 = @"This is a string";  
    168. NSString *string2 = [string1 substringToIndex:3];  
    169. NSLog(@"string2:%@",string2);  
    170.   
    171.   
    172.   
    173.   
    174. //-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符  
    175. NSString *string1 = @"This is a string";  
    176. NSString *string2 = [string1 substringFromIndex:3];  
    177. NSLog(@"string2:%@",string2);  
    178.   
    179.   
    180.   
    181.   
    182. //-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串  
    183. NSString *string1 = @"This is a string";  
    184. NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];  
    185. NSLog(@"string2:%@",string2);  
    186.   
    187.   
    188. //扩展路径  
    189.   
    190. NSString *Path = @"~/NSData.txt";  
    191. NSString *absolutePath = [Path stringByExpandingTildeInPath];  
    192. NSLog(@"absolutePath:%@",absolutePath);  
    193. NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);  
    194.   
    195.   
    196.   
    197. //文件扩展名  
    198. NSString *Path = @"~/NSData.txt";  
    199. NSLog(@"Extension:%@",[Path pathExtension]);  
    200.   
    201.   
    202.   
    203.   
    204. /*******************************************************************************************  
    205.  NSMutableString  
    206.  *******************************************************************************************/      
    207.   
    208. /*---------------给字符串分配容量----------------*/  
    209. //stringWithCapacity:  
    210. NSMutableString *String;  
    211. String = [NSMutableString stringWithCapacity:40];  
    212.   
    213.   
    214. /*---------------在已有字符串后面添加字符----------------*/      
    215.   
    216. //appendString: and appendFormat:  
    217.   
    218. NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];  
    219. //[String1 appendString:@", I will be adding some character"];  
    220. [String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];  
    221. NSLog(@"String1:%@",String1);  
    222. */  
    223.   
    224.   
    225. /*--------在已有字符串中按照所给出范围和长度删除字符------*/      
    226. /*  
    227.  //deleteCharactersInRange:  
    228.  NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];  
    229.  [String1 deleteCharactersInRange:NSMakeRange(0, 5)];  
    230.  NSLog(@"String1:%@",String1);  
    231.   
    232.   
    233.   
    234.  /*--------在已有字符串后面在所指定的位置中插入给出的字符串------*/  
    235.   
    236. //-insertString: atIndex:  
    237. NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];  
    238. [String1 insertString:@"Hi! " atIndex:0];  
    239. NSLog(@"String1:%@",String1);  
    240.   
    241.   
    242.   
    243. /*--------将已有的空符串换成其它的字符串------*/  
    244.   
    245. //-setString:  
    246. NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];  
    247. [String1 setString:@"Hello Word!"];  
    248. NSLog(@"String1:%@",String1);  
    249.   
    250.   
    251.   
    252. /*--------按照所给出的范围,和字符串替换的原有的字符------*/  
    253.   
    254. //-setString:  
    255. NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];  
    256. [String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];  
    257. NSLog(@"String1:%@",String1);  
    258.   
    259.   
    260.   
    261. /*-------------判断字符串内是否还包含别的字符串(前缀,后缀)-------------*/  
    262. //01:检查字符串是否以另一个字符串开头- (BOOL) hasPrefix: (NSString *) aString;  
    263. NSString *String1 = @"NSStringInformation.txt";  
    264. [String1 hasPrefix:@"NSString"] = = 1 ?  NSLog(@"YES") : NSLog(@"NO");  
    265. [String1 hasSuffix:@".txt"] = = 1 ?  NSLog(@"YES") : NSLog(@"NO");  
    266.   
    267. //02:查找字符串某处是否包含其它字符串 - (NSRange) rangeOfString: (NSString *) aString,这一点前面在串中搜索子串用到过;  
    268.   
    269.   
    270.   
    271. /*******************************************************************************************  
    272.  NSArray  
    273.  *******************************************************************************************/  
    274.   
    275. /*---------------------------创建数组------------------------------*/  
    276. //NSArray *array = [[NSArray alloc] initWithObjects:  
    277. @"One",@"Two",@"Three",@"Four",nil];  
    278.   
    279. self.dataArray = array;  
    280. [array release];  
    281.   
    282. //- (unsigned) Count;数组所包含对象个数;  
    283. NSLog(@"self.dataArray cound:%d",[self.dataArray count]);  
    284.   
    285. //- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;  
    286. NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);  
    287.   
    288.   
    289. /*--------------------------从一个数组拷贝数据到另一数组(可变数级)----------------------------*/      
    290.   
    291. //arrayWithArray:  
    292. //NSArray *array1 = [[NSArray alloc] init];  
    293. NSMutableArray *MutableArray = [[NSMutableArray alloc] init];  
    294. NSArray *array = [NSArray arrayWithObjects:  
    295.                   @"a",@"b",@"c",nil];  
    296. NSLog(@"array:%@",array);  
    297. MutableArray = [NSMutableArray arrayWithArray:array];  
    298. NSLog(@"MutableArray:%@",MutableArray);  
    299.   
    300. array1 = [NSArray arrayWithArray:array];  
    301. NSLog(@"array1:%@",array1);  
    302.   
    303.   
    304. //Copy  
    305.   
    306. //id obj;  
    307. NSMutableArray *newArray = [[NSMutableArray alloc] init];  
    308. NSArray *oldArray = [NSArray arrayWithObjects:  
    309.                      @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];  
    310.   
    311. NSLog(@"oldArray:%@",oldArray);  
    312. for(int i = 0; i < [oldArray count]; i++)  
    313. {          
    314.     obj = [[oldArray objectAtIndex:i] copy];  
    315.     [newArray addObject: obj];  
    316. }  
    317. //       
    318. NSLog(@"newArray:%@", newArray);  
    319. [newArray release];  
    320.   
    321.   
    322. //快速枚举  
    323.   
    324. //NSMutableArray *newArray = [[NSMutableArray alloc] init];  
    325. NSArray *oldArray = [NSArray arrayWithObjects:  
    326.                      @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];      
    327. NSLog(@"oldArray:%@",oldArray);  
    328.   
    329. for(id obj in oldArray)  
    330. {  
    331.     [newArray addObject: obj];  
    332. }  
    333. //       
    334. NSLog(@"newArray:%@", newArray);  
    335. [newArray release];      
    336.   
    337.   
    338. //Deep copy  
    339.   
    340. //NSMutableArray *newArray = [[NSMutableArray alloc] init];  
    341. NSArray *oldArray = [NSArray arrayWithObjects:  
    342.                      @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];      
    343. NSLog(@"oldArray:%@",oldArray);      
    344. newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);  
    345. NSLog(@"newArray:%@", newArray);  
    346. [newArray release];      
    347.   
    348.   
    349. //Copy and sort  
    350.   
    351. //NSMutableArray *newArray = [[NSMutableArray alloc] init];  
    352. NSArray *oldArray = [NSArray arrayWithObjects:  
    353.                      @"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];      
    354. NSLog(@"oldArray:%@",oldArray);  
    355. NSEnumerator *enumerator;  
    356. enumerator = [oldArray objectEnumerator];  
    357. id obj;  
    358. while(obj = [enumerator nextObject])  
    359. {  
    360.     [newArray addObject: obj];  
    361. }  
    362. [newArray sortUsingSelector:@selector(compare:)];  
    363. NSLog(@"newArray:%@", newArray);  
    364. [newArray release];  
    365.   
    366.   
    367.   
    368. /*---------------------------切分数组------------------------------*/  
    369.   
    370. //从字符串分割到数组- componentsSeparatedByString:  
    371. NSString *string = [[NSString alloc] initWithString:@"One,Two,Three,Four"];  
    372. NSLog(@"string:%@",string);      
    373. NSArray *array = [string componentsSeparatedByString:@","];  
    374. NSLog(@"array:%@",array);  
    375. [string release];  
    376.   
    377.   
    378. //从数组合并元素到字符串- componentsJoinedByString:  
    379. NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];  
    380. NSString *string = [array componentsJoinedByString:@","];  
    381. NSLog(@"string:%@",string);  
    382.   
    383.   
    384.   
    385. /*******************************************************************************************  
    386.  NSMutableArray  
    387.  *******************************************************************************************/  
    388. /*---------------给数组分配容量----------------*/  
    389. //NSArray *array;  
    390. array = [NSMutableArray arrayWithCapacity:20];  
    391.   
    392.   
    393.   
    394. /*--------------在数组末尾添加对象----------------*/  
    395. //- (void) addObject: (id) anObject;  
    396. //NSMutableArray *array = [NSMutableArray arrayWithObjects:  
    397. @"One",@"Two",@"Three",nil];  
    398. [array addObject:@"Four"];  
    399. NSLog(@"array:%@",array);  
    400.   
    401.   
    402.   
    403. /*--------------删除数组中指定索引处对象----------------*/      
    404. //-(void) removeObjectAtIndex: (unsigned) index;      
    405. //NSMutableArray *array = [NSMutableArray arrayWithObjects:  
    406. @"One",@"Two",@"Three",nil];  
    407. [array removeObjectAtIndex:1];  
    408. NSLog(@"array:%@",array);  
    409.   
    410.   
    411.   
    412. /*-------------数组枚举---------------*/      
    413. //- (NSEnumerator *)objectEnumerator;从前向后  
    414. //NSMutableArray *array = [NSMutableArray arrayWithObjects:  
    415. @"One",@"Two",@"Three",nil];  
    416. NSEnumerator *enumerator;  
    417. enumerator = [array objectEnumerator];  
    418.   
    419. id thingie;  
    420. while (thingie = [enumerator nextObject]) {  
    421.     NSLog(@"thingie:%@",thingie);  
    422. }  
    423.   
    424.   
    425. //- (NSEnumerator *)reverseObjectEnumerator;从后向前  
    426. //NSMutableArray *array = [NSMutableArray arrayWithObjects:  
    427. @"One",@"Two",@"Three",nil];  
    428. NSEnumerator *enumerator;  
    429. enumerator = [array reverseObjectEnumerator];  
    430.   
    431. id object;  
    432. while (object = [enumerator nextObject]) {  
    433.     NSLog(@"object:%@",object);  
    434. }  
    435.   
    436.   
    437. //快速枚举  
    438. //NSMutableArray *array = [NSMutableArray arrayWithObjects:  
    439. @"One",@"Two",@"Three",nil];  
    440. for(NSString *string in array)  
    441. {  
    442.     NSLog(@"string:%@",string);  
    443. }  
    444.   
    445.   
    446.   
    447. /*******************************************************************************************  
    448.  NSDictionary  
    449.  *******************************************************************************************/  
    450.   
    451. /*------------------------------------创建字典------------------------------------*/  
    452. //- (id) initWithObjectsAndKeys;  
    453.   
    454. //NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];  
    455. NSString *string = [dictionary objectForKey:@"One"];  
    456. NSLog(@"string:%@",string);  
    457. NSLog(@"dictionary:%@",dictionary);  
    458. [dictionary release];  
    459.   
    460.   
    461. /*******************************************************************************************  
    462.  NSMutableDictionary  
    463.  *******************************************************************************************/  
    464.   
    465. /*------------------------------------创建可变字典------------------------------------*/      
    466. //创建  
    467. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];  
    468.   
    469. //添加字典  
    470. [dictionary setObject:@"One" forKey:@"1"];  
    471. [dictionary setObject:@"Two" forKey:@"2"];  
    472. [dictionary setObject:@"Three" forKey:@"3"];  
    473. [dictionary setObject:@"Four" forKey:@"4"];  
    474. NSLog(@"dictionary:%@",dictionary);  
    475.   
    476. //删除指定的字典  
    477. [dictionary removeObjectForKey:@"3"];  
    478. NSLog(@"dictionary:%@",dictionary);  
    479.   
    480.   
    481. /*******************************************************************************************  
    482.  NSValue(对任何对象进行包装)  
    483.  *******************************************************************************************/  
    484.   
    485. /*--------------------------------将NSRect放入NSArray中------------------------------------*/      
    486. //将NSRect放入NSArray中  
    487. NSMutableArray *array = [[NSMutableArray alloc] init];  
    488. NSValue *value;  
    489. CGRect rect = CGRectMake(0, 0, 320, 480);      
    490. value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];  
    491. [array addObject:value];  
    492. NSLog(@"array:%@",array);  
    493.   
    494. //从Array中提取  
    495. value = [array objectAtIndex:0];  
    496. [value getValue:&rect];  
    497. NSLog(@"value:%@",value);  
    498.   
    499.   
    500. /*******************************************************************************************  
    501.  从目录搜索扩展名为jpg的文件  
    502.  *******************************************************************************************/  
    503.   
    504. //NSFileManager *fileManager = [NSFileManager defaultManager];  
    505. NSString *home;  
    506. home = @"../Users/";  
    507.   
    508. NSDirectoryEnumerator *direnum;  
    509. direnum = [fileManager enumeratorAtPath: home];  
    510.   
    511. NSMutableArray *files = [[NSMutableArray alloc] init];  
    512.   
    513. //枚举  
    514. NSString *filename;  
    515. while (filename = [direnum nextObject]) {  
    516.     if([[filename pathExtension] hasSuffix:@"jpg"]){  
    517.         [files addObject:filename];  
    518.     }  
    519. }  
    520.   
    521. //快速枚举  
    522. //for(NSString *filename in direnum)  
    523. //{  
    524. //    if([[filename pathExtension] isEqualToString:@"jpg"]){  
    525. //        [files addObject:filename];  
    526. //    }  
    527. //}  
    528. NSLog(@"files:%@",files);  
    529.   
    530. //枚举  
    531. NSEnumerator *filenum;  
    532. filenum = [files objectEnumerator];  
    533. while (filename = [filenum nextObject]) {  
    534.     NSLog(@"filename:%@",filename);  
    535. }  
    536.   
    537. //快速枚举  
    538. //for(id object in files)  
    539. //{  
    540. //    NSLog(@"object:%@",object);  
    541. //} 
  • 相关阅读:
    调试JavaScript错误的有效方法 利用firebug
    让网站支持RSS订阅
    解决通过Cookie进行网站自动登录的安全性问题
    TinyMce编辑器的简体中文和字体太小补丁
    CSS: 简单行列用table还是dl,dt,dd?
    让tinyMce输出<pre>标签html代码自动换行
    云计算产生的背景
    云的一二三四五
    未绑定元素“mx:Panel”的前缀“mx”
    windows下如何下载android源码
  • 原文地址:https://www.cnblogs.com/isItOk/p/5218787.html
Copyright © 2020-2023  润新知