iOS NSString+NSMutableString+NSValue+NSArry用法汇总

来源:互联网 发布:天津老年大学网络报名 编辑:程序博客网 时间:2024/05/29 13:47
[cpp] view plaincopy
  1. NSString+NSMutableString+NSValue+NSAraay用法汇总  
[cpp] view plaincopy
  1. /******************************************************************************************* 
  2.      NSString 
  3.      *******************************************************************************************/  
  4.     //一、NSString      
  5.     /*----------------创建字符串的方法----------------*/  
  6.     

    deviceToken 转成字符串:

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