天天看點

NSString+NSMutableString+NSValue+NSAraay用法彙總

//一、NSString   

    /*----------------建立字元串的方法----------------*/

    //1、建立常量字元串。

    NSString *astring = @"This is a String!";

    //2、建立空字元串,給予指派。

    NSString *astring = [[NSString alloc] init];

    astring = @"This is a String!";

    NSLog(@"astring:%@",astring);

  [astring release];

    //3、在以上方法中,提升速度:initWithString方法

    NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];

    [astring release];

    //4、用标準c建立字元串:initWithCString方法

    char *Cstring = "This is a String!";

    NSString *astring = [[NSString alloc] initWithCString:Cstring];

    //5、建立格式化字元串:占位符(由一個%加一個字元組成)

    int i = 1;

    int j = 2;

    NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];

    //6、建立臨時字元串

    NSString *astring;

    astring = [NSString stringWithCString:"This is a temporary string"];

    /*----------------從檔案讀取字元串:initWithContentsOfFile方法 ----------------*/   

    NSString *path = @"astring.text";

    NSString *astring = [[NSString alloc] initWithContentsOfFile:path];

    /*----------------寫字元串到檔案:writeToFile方法 ----------------*/   

    NSString *path = @"astring.text";   

    [astring writeToFile: path atomically: YES];

    [astring release];   

    /*---------------- 比較兩個字元串----------------*/        

    //用C比較:strcmp函數

    char string1[] = "string!";

    char string2[] = "string!";

    if(strcmp(string1, string2) = = 0)

    {

        NSLog(@"1");

    }

    //isEqualToString方法   

    NSString *astring01 = @"This is a String!";

    NSString *astring02 = @"This is a String!";

    BOOL result = [astring01 isEqualToString:astring02];

    NSLog(@"result:%d",result);

    //compare方法(comparer傳回的三種值)   

    NSString *astring02 = @"This is a String!";   

    BOOL result = [astring01 compare:astring02] = = NSOrderedSame;   

    NSLog(@"result:%d",result);   

    //NSOrderedSame 判斷兩者内容是否相同

    NSString *astring02 = @"this is a String!";

    BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;   

    //NSOrderedAscending 判斷兩對象值的大小(按字母順序進行比較,astring02大于astring01為真)

    NSString *astring01 = @"this is a String!";

    BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;   

    NSLog(@"result:%d",result);     

    //NSOrderedDescending 判斷兩對象值的大小(按字母順序進行比較,astring02小于astring01為真)

    //不考慮大 小寫比較字元串1

    BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;   

    //NSOrderedDescending判斷兩對象值的大小(按字母順序進行比較,astring02小于astring01為 真)

    //不考慮大小寫比較字元串2

    BOOL result = [astring01 compare:astring02

                            options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;   

    //NSCaseInsensitiveSearch:不區分大小寫比較 NSLiteralSearch:進行完全比較,區分大小寫 NSNumericSearch:比較字元串的字元個數,而不是字元值。

    /*----------------改變字元串的大小寫----------------*/   

    NSString *string1 = @"A String";

    NSString *string2 = @"String";

    NSLog(@"string1:%@",[string1 uppercaseString]);//大寫

    NSLog(@"string2:%@",[string2 lowercaseString]);//小寫

    NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小

    /*----------------在串中搜尋子串 ----------------*/        

    NSString *string1 = @"This is a string";

    NSString *string2 = @"string";

    NSRange range = [string1 rangeOfString:string2];

    int location = range.location;

    int leight = range.length;

    NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];

    /*----------------抽取子串 ----------------*/        

    //-substringToIndex: 從字元串的開頭一直截取到指定的位置,但不包括該位置的字元

    NSString *string2 = [string1 substringToIndex:3];

    NSLog(@"string2:%@",string2);

    //-substringFromIndex: 以指定位置開始(包括指定位置的字元),并包括之後的全部字元

    NSString *string2 = [string1 substringFromIndex:3];

    //-substringWithRange: //按照所給出的位置,長度,任意地從字元串中截取子串

    NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];

//快速枚舉

    //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);

@"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"];

    /*-------------- 删除數組中指定索引處對象----------------*/   

    //-(void) removeObjectAtIndex: (unsigned) index;   

    [array removeObjectAtIndex:1];

    /*------------- 數組枚舉---------------*/   

    //- (NSEnumerator *)objectEnumerator;從前向後

    enumerator = [array objectEnumerator];

    id thingie;

    while (thingie = [enumerator nextObject]) {

        NSLog(@"thingie:%@",thingie);

    //- (NSEnumerator *)reverseObjectEnumerator;從後向前

    enumerator = [array reverseObjectEnumerator];

    id object;

    while (object = [enumerator nextObject]) {

        NSLog(@"object:%@",object);

    //快速枚舉

    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(@"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"];

    //删除指定的字典

    [dictionary removeObjectForKey:@"3"];

    /******************************************************************************

     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];

    //從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];

        }

//擴充路徑

    NSString *Path = @"~/NSData.txt";

    NSString *absolutePath = [Path stringByExpandingTildeInPath];

    NSLog(@"absolutePath:%@",absolutePath);

    NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);

    //檔案擴充名

    NSLog(@"Extension:%@",[Path pathExtension]);

    /***********************************************************************

     NSMutableString

     ***********************************************************************/   

    /*---------------給字元串配置設定容量----------------*/

    //stringWithCapacity:

    NSMutableString *String;

    String = [NSMutableString stringWithCapacity:40];

    /*---------------在已有字元串後面添加字元----------------*/   

    //appendString: and appendFormat:

    NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];

    //[String1 appendString:@", I will be adding some character"];

    [String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];

    NSLog(@"String1:%@",String1);

    */

    /*-------- 在已有字元串中按照所給出範圍和長度删除字元------*/   

    /*

     //deleteCharactersInRange:

     NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];

     [String1 deleteCharactersInRange:NSMakeRange(0, 5)];

     NSLog(@"String1:%@",String1);

     /*--------在已有字元串後面在所指定的位置中插入給出的字元串------*/

    //-insertString: atIndex:

    [String1 insertString:@"Hi! " atIndex:0];

    /*--------将已有的空符串換成其它的字元串------*/

    //-setString:

    [String1 setString:@"Hello Word!"];

    /*--------按照所給出的範圍,和字元串替換的原有的字元------*/

    [String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];

    /*-------------判斷字元串内是否還包含别的字元串(字首,字尾)-------------*/

    //01: 檢查字元串是否以另一個字元串開頭- (BOOL) hasPrefix: (NSString *) aString;

    NSString *String1 = @"NSStringInformation.txt";

    [String1 hasPrefix:@"NSString"] = = 1 ?  NSLog(@"YES") : NSLog(@"NO");

    [String1 hasSuffix:@".txt"] = = 1 ?  NSLog(@"YES") : NSLog(@"NO");

    //02: 查找字元串某處是否包含其它字元串 - (NSRange) rangeOfString: (NSString *) aString,這一點前面在串中搜尋子串用到過;

     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];

    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];

    for(int i = 0; i < [oldArray count]; i++)

    {        

        obj = [[oldArray objectAtIndex:i] copy];

    //     

    //NSMutableArray *newArray = [[NSMutableArray alloc] init];

                         @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];   

    for(id obj in oldArray)

    [newArray release];   

    //Deep copy

    NSLog(@"oldArray:%@",oldArray);   

    newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);

    //Copy and sort

轉載:http://www.cnblogs.com/zhw511006/archive/2010/09/14/1825648.html

     本文轉自新風作浪 51CTO部落格,原文連結:http://blog.51cto.com/duxinfeng/1208783,如需轉載請自行聯系原作者

繼續閱讀