zoukankan      html  css  js  c++  java
  • NSString和NSMutableString常用方法+NSArray常用代码 (转)


    常见的NSString和NSMutableString方法:

    NSString方法:

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

     

    NSArray的基本操作代码:

     

    [plain] view plaincopy
     
      1. /*---------------------------创建数组------------------------------*/  
      2.     //NSArray *array = [NSArray alloc] initWithObjects:  
      3.     @"One",@"Two",@"Three",@"Four",nil];  
      4.   
      5.     self.dataArray = array;  
      6.     [array release];  
      7.   
      8.     //- (unsigned) Count;数组所包含对象个数;  
      9.     NSLog(@"self.dataArray cound:%d",[self.dataArray count]);  
      10.   
      11.     //- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;  
      12.     NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);  
      13.   
      14.   
      15.     /*--------------------------从一个数组拷贝数据到另一数组(可变数级)----------------------------*/      
      16.   
      17.     //arrayWithArray:  
      18.     //NSArray *array1 = [NSArray alloc] init];  
      19.     NSMutableArray *MutableArray = [NSMutableArray alloc] init];  
      20.     NSArray *array = [NSArray arrayWithObjects:  
      21.                       @"a",@"b",@"c",nil];  
      22.     NSLog(@"array:%@",array);  
      23.     MutableArray = [NSMutableArray arrayWithArray:array];  
      24.     NSLog(@"MutableArray:%@",MutableArray);  
      25.   
      26.     array1 = [NSArray arrayWithArray:array];  
      27.     NSLog(@"array1:%@",array1);  
      28.   
      29.   
      30.     //Copy  
      31.   
      32.     //id obj;  
      33.     NSMutableArray *newArray = [NSMutableArray alloc] init];  
      34.     NSArray *oldArray = [NSArray arrayWithObjects:  
      35.                          @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];  
      36.   
      37.     NSLog(@"oldArray:%@",oldArray);  
      38.     for(int i = 0; i < [oldArray count]; i++)  
      39.     {          
      40.         obj = [oldArray objectAtIndex:i] copy];  
      41.         [newArray addObject: obj];  
      42.     }  
      43.     //       
      44.     NSLog(@"newArray:%@", newArray);  
      45.     [newArray release];  
      46.   
      47.   
      48.     //快速枚举  
      49.   
      50.     //NSMutableArray *newArray = [NSMutableArray alloc] init];  
      51.     NSArray *oldArray = [NSArray arrayWithObjects:  
      52.                          @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];      
      53.     NSLog(@"oldArray:%@",oldArray);  
      54.   
      55.     for(id obj in oldArray)  
      56.     {  
      57.         [newArray addObject: obj];  
      58.     }  
      59.     //       
      60.     NSLog(@"newArray:%@", newArray);  
      61.     [newArray release];      
      62.   
      63.   
      64.     //Deep copy  
      65.   
      66.     //NSMutableArray *newArray = [NSMutableArray alloc] init];  
      67.     NSArray *oldArray = [NSArray arrayWithObjects:  
      68.                          @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];      
      69.     NSLog(@"oldArray:%@",oldArray);      
      70.     newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);  
      71.     NSLog(@"newArray:%@", newArray);  
      72.     [newArray release];      
      73.   
      74.   
      75.     //Copy and sort  
      76.   
      77.     //NSMutableArray *newArray = [NSMutableArray alloc] init];  
      78.     NSArray *oldArray = [NSArray arrayWithObjects:  
      79.                          @"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];      
      80.     NSLog(@"oldArray:%@",oldArray);  
      81.     NSEnumerator *enumerator;  
      82.     enumerator = [oldArray objectEnumerator];  
      83.     id obj;  
      84.     while(obj = [enumerator nextObject])  
      85.     {  
      86.         [newArray addObject: obj];  
      87.     }  
      88.     [newArray sortUsingSelector:@selector(compare:)];  
      89.     NSLog(@"newArray:%@", newArray);  
      90.     [newArray release];  
      91.   
      92.   
      93.   
      94.     /*---------------------------切分数组------------------------------*/  
      95.   
      96.     //从字符串分割到数组- componentsSeparatedByString:  
      97.     NSString *string = [NSString alloc] initWithString:@"One,Two,Three,Four"];  
      98.     NSLog(@"string:%@",string);      
      99.     NSArray *array = [string componentsSeparatedByString:@","];  
      100.     NSLog(@"array:%@",array);  
      101.     [string release];  
      102.   
      103.   
      104.     //从数组合并元素到字符串- componentsJoinedByString:  
      105.     NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];  
      106.     NSString *string = [array componentsJoinedByString:@","];  
      107.     NSLog(@"string:%@",string);  
      108.   
      109.   
      110.   
      111.     /*******************************************************************************************  
      112.      NSMutableArray  
      113.      *******************************************************************************************/  
      114.     /*---------------给数组分配容量----------------*/  
      115.     //NSArray *array;  
      116.     array = [NSMutableArray arrayWithCapacity:20];  
      117.   
      118.   
      119.   
      120.     /*--------------在数组末尾添加对象----------------*/  
      121.     //- (void) addObject: (id) anObject;  
      122.     //NSMutableArray *array = [NSMutableArray arrayWithObjects:  
      123.     @"One",@"Two",@"Three",nil];  
      124.     [array addObject:@"Four"];  
      125.     NSLog(@"array:%@",array);  
      126.   
      127.   
      128.   
      129.     /*--------------删除数组中指定索引处对象----------------*/      
      130.     //-(void) removeObjectAtIndex: (unsigned) index;      
      131.     //NSMutableArray *array = [NSMutableArray arrayWithObjects:  
      132.     @"One",@"Two",@"Three",nil];  
      133.     [array removeObjectAtIndex:1];  
      134.     NSLog(@"array:%@",array);  
      135.   
      136.   
      137.   
      138.     /*-------------数组枚举---------------*/      
      139.     //- (NSEnumerator *)objectEnumerator;从前向后  
      140.     //NSMutableArray *array = [NSMutableArray arrayWithObjects:  
      141.     @"One",@"Two",@"Three",nil];  
      142.     NSEnumerator *enumerator;  
      143.     enumerator = [array objectEnumerator];  
      144.   
      145.     id thingie;  
      146.     while (thingie = [enumerator nextObject]) {  
      147.         NSLog(@"thingie:%@",thingie);  
      148.     }  
      149.   
      150.   
      151.     //- (NSEnumerator *)reverseObjectEnumerator;从后向前  
      152.     //NSMutableArray *array = [NSMutableArray arrayWithObjects:  
      153.     @"One",@"Two",@"Three",nil];  
      154.     NSEnumerator *enumerator;  
      155.     enumerator = [array reverseObjectEnumerator];  
      156.   
      157.     id object;  
      158.     while (object = [enumerator nextObject]) {  
      159.         NSLog(@"object:%@",object);  
      160.     }  
      161.   
      162.   
      163.     //快速枚举  
      164.     //NSMutableArray *array = [NSMutableArray arrayWithObjects:  
      165.     @"One",@"Two",@"Three",nil];  
      166.     for(NSString *string in array)  
      167.     {  
      168.         NSLog(@"string:%@",string);  
      169.     }  
      170.   
      171.   
      172.   
      173.     /*******************************************************************************************  
      174.      NSDictionary  
      175.      *******************************************************************************************/  
      176.   
      177.     /*------------------------------------创建字典------------------------------------*/  
      178.     //- (id) initWithObjectsAndKeys;  
      179.   
      180.     //NSDictionary *dictionary = [NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];  
      181.     NSString *string = [dictionary objectForKey:@"One"];  
      182.     NSLog(@"string:%@",string);  
      183.     NSLog(@"dictionary:%@",dictionary);  
      184.     [dictionary release];  
      185.   
      186.   
      187.     /*******************************************************************************************  
      188.      NSMutableDictionary  
      189.      *******************************************************************************************/  
      190.   
      191.     /*------------------------------------创建可变字典------------------------------------*/      
      192.     //创建  
      193.     NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];  
      194.   
      195.     //添加字典  
      196.     [dictionary setObject:@"One" forKey:@"1"];  
      197.     [dictionary setObject:@"Two" forKey:@"2"];  
      198.     [dictionary setObject:@"Three" forKey:@"3"];  
      199.     [dictionary setObject:@"Four" forKey:@"4"];  
      200.     NSLog(@"dictionary:%@",dictionary);  
      201.   
      202.     //删除指定的字典  
      203.     [dictionary removeObjectForKey:@"3"];  
      204.     NSLog(@"dictionary:%@",dictionary);  
      205.   
      206.   
      207.     /*******************************************************************************************  
      208.      NSValue(对任何对象进行包装)  
      209.      *******************************************************************************************/  
      210.   
      211.     /*--------------------------------将NSRect放入NSArray中------------------------------------*/      
      212.     //将NSRect放入NSArray中  
      213.     NSMutableArray *array = [NSMutableArray alloc] init];  
      214.     NSValue *value;  
      215.     CGRect rect = CGRectMake(0, 0, 320, 480);      
      216.     value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];  
      217.     [array addObject:value];  
      218.     NSLog(@"array:%@",array);  
      219.   
      220.     //从Array中提取  
      221.     value = [array objectAtIndex:0];  
      222.     [value getValue:&rect];  
      223.     NSLog(@"value:%@",value);  
      224.   
      225.   
      226.     /*******************************************************************************************  
      227.      从目录搜索扩展名为jpg的文件  
      228.      *******************************************************************************************/  
      229.   
      230.     //NSFileManager *fileManager = [NSFileManager defaultManager];  
      231.     NSString *home;  
      232.     home = @"../Users/";  
      233.   
      234.     NSDirectoryEnumerator *direnum;  
      235.     direnum = [fileManager enumeratorAtPath: home];  
      236.   
      237.     NSMutableArray *files = [NSMutableArray alloc] init];  
      238.   
      239.     //枚举  
      240.     NSString *filename;  
      241.     while (filename = [direnum nextObject]) {  
      242.         if([filename pathExtension] hasSuffix:@"jpg"]){  
      243.             [files addObject:filename];  
      244.         }  
      245.     }  
      246.   
      247.     //快速枚举  
      248.     //for(NSString *filename in direnum)  
      249.     //{  
      250.     //    if([filename pathExtension] isEqualToString:@"jpg"]){  
      251.     //        [files addObject:filename];  
      252.     //    }  
      253.     //}  
      254.     NSLog(@"files:%@",files);  
      255.   
      256.     //枚举  
      257.     NSEnumerator *filenum;  
      258.     filenum = [files objectEnumerator];  
      259.     while (filename = [filenum nextObject]) {  
      260.         NSLog(@"filename:%@",filename);  
      261.     }  
      262.   
      263.     //快速枚举  
      264.     //for(id object in files)  
      265.     //{  
      266.     //    NSLog(@"object:%@",object);  
      267.     //}
  • 相关阅读:
    转: IOS程序内发短信 MFMessageComposeViewController
    对象之间如何比较是否相等?
    相比xib 使用代码编排view 的一个明显的好处就是可以更好地重复使用已有代码,减少代码冗余。
    关于deselectRowAtIndexPath
    IOS中为tableViewCell增加右侧标记(选中或者更多)
    ios 中是否每一个对象(尤其是在使用多线程时),都要判断一下对象是否为nil,以防止程序闪退?
    模拟出ios中流行的黑色背景底
    转 ios给view设置圆角
    转 UIAlertView 不显示、屏幕变灰
    转 UIActivityIndicatorView、UIProgressView 活动与进度指示器-IOS开发
  • 原文地址:https://www.cnblogs.com/luluping/p/3751841.html
Copyright © 2011-2022 走看看