Object-c 의 문자열 NSString,NSMutableString 요약

    //   :http://www.myexception.cn/mobile/455287.html
    // NSString.html        :
    // https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
    
    // 、NSString     ,     
    //1、       。
    NSString *astring = @"This is a String!";
    
    //2、      ,    。
    NSString *astring = [[NSString alloc] init];
    astring = @"This is a String!";
    [astring release];
    NSLog(@"astring:%@",astring);
    
    //3、      ,    :initWithString  
    NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
    NSLog(@"astring:%@",astring);
    [astring release];
    
    //4、   c     :initWithCString  
    char *Cstring = "This is a String!";
    NSString *astring = [[NSString alloc] initWithCString:Cstring];
    NSLog(@"astring:%@",astring);
    [astring release];
    
    //5、        :   (   %       )
    int i = 1;
    int j = 2;
    NSString *astring = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];
    NSLog(@"astring:%@",astring);
    [astring release];
    
    //6、       
    NSString *astring;
    astring = [NSString stringWithCString:"This is a temporary string"];
    NSLog(@"astring:%@",astring);
    
    //7、    
    NSUInteger uLen1 = [astring length];
    NSLog(@"uLen = %lu", uLen1);
    
    //8、          
    NSUInteger index = 0;
    unichar c = [sFormat characterAtIndex:index];
    NSLog(@"The character at index %u of \"%@\" is \'%c\'", index, sFormat, c);
    
    //9、        :initWithContentsOfFile  
    NSString *path = @"astring.text";
    NSString *astring = [[NSString alloc] initWithContentsOfFile:path];
    NSLog(@"astring:%@",astring);
    [astring release];
    
    // 10、       :writeToFile  
    NSString *astring = [[NSString alloc] initWithString:@"This is a String!"];
    NSLog(@"astring:%@",astring);
    NSString *path = @"astring.text";
    [astring writeToFile: path atomically: YES];
    [astring release];
    
    //11、       
    // 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      ,    -1,    0,    1)
    NSString *astring01 = @"This is a String!";
    NSString *astring02 = @"This is a String!";
    BOOL result = [astring01 compare:astring02] = = NSOrderedSame;
    NSLog(@"result:%d",result);
    
    //NSOrderedSame          
    NSString *astring01 = @"This is a String!";
    NSString *astring02 = @"this is a String!";
    BOOL result = [astring01 compare:astring02] = = NSOrderedAscending;
    NSLog(@"result:%d",result);
    
    //NSOrderedAscending         (         ,astring02  astring01  )
    NSString *astring01 = @"this is a String!";
    NSString *astring02 = @"This is a String!";
    BOOL result = [astring01 compare:astring02] = = NSOrderedDescending;
    NSLog(@"result:%d",result);
    
    //NSOrderedDescending         (         ,astring02  astring01  )
    //           1
    NSString *astring01 = @"this is a String!";
    NSString *astring02 = @"This is a String!";
    BOOL result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame;
    NSLog(@"result:%d",result);
    
    //NSOrderedDescending         (         ,astring02  astring01  )
    //           2
    NSString *astring01 = @"this is a String!";
    NSString *astring02 = @"This is a String!";
    BOOL result = [astring01 compare:astring02
                             options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame;
    NSLog(@"result:%d",result);
    
    //NSCaseInsensitiveSearch:         NSLiteralSearch:      ,      NSNumericSearch:          ,      。
    
    //12、         
    NSString *string1 = @"A String";
    NSString *string2 = @"String";
    NSLog(@"string1:%@",[string1 uppercaseString]);//  
    NSLog(@"string2:%@",[string2 lowercaseString]);//  
    NSLog(@"string2:%@",[string2 capitalizedString]);//     
    
    //13、       
    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]];
    NSLog(@"astring:%@",astring);
    [astring release];
    
    //14、    
    //-substringToIndex:                  ,          
    NSString *string1 = @"This is a string";
    NSString *string2 = [string1 substringToIndex:3];
    NSLog(@"string2:%@",string2);
    
    //-substringFromIndex:        (         ),          
    NSString *string1 = @"This is a string";
    NSString *string2 = [string1 substringFromIndex:3];
    NSLog(@"string2:%@",string2);
    //-substringWithRange: //        ,  ,            
    NSString *string1 = @"This is a string";
    NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 4)];
    NSLog(@"string2:%@",string2);
    
    //15、    
    NSString *Path = @"~/NSData.txt";
    NSString *absolutePath = [Path stringByExpandingTildeInPath];
    NSLog(@"absolutePath:%@",absolutePath);
    NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
    
    //16、     
    NSString *Path = @"~/NSData.txt";
    NSLog(@"Extension:%@",[Path pathExtension]);
    
    // 、NSMutableString           
    //       :https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableString
    /*******************************************************************************************
     NSMutableString
     *******************************************************************************************/
    
    /*---------------        ----------------*/
    //1、stringWithCapacity:
    NSMutableString *String;
    String = [NSMutableString stringWithCapacity:40];
    
    //2、            
    //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);
    
    //3、                     
    //deleteCharactersInRange:
    NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
    [String1 deleteCharactersInRange:NSMakeRange(0, 5)];
    NSLog(@"String1:%@",String1);
    
    //4、                        
    //-insertString: atIndex:
    NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
    [String1 insertString:@"Hi! " atIndex:0];
    NSLog(@"String1:%@",String1);
    
    //5、               
    //-setString:
    NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
    [String1 setString:@"Hello Word!"];
    NSLog(@"String1:%@",String1);
    
    //6、        ,            
    //-setString:
    NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
    [String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];
    NSLog(@"String1:%@",String1);
    
    //7、                (  ,  )
    //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,               ;

좋은 웹페이지 즐겨찾기