NSString

1. 折分字符串操作:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    //将一个句子分解为单独的单词 
    NSString *string1 = @"my name is bill";
    NSArray *words = [string1 componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSLog(@"Word array: %@", words);
    
    //把一个NSString追加到另一个NSString
    NSString *str1 = @"第一个字符串 ";
    NSString *str2 = @"第二个字符串。";
    NSString *result = [str1 stringByAppendingFormat:str2];
    NSLog(@"result: %@", result);

    [pool drain];
    return 0;
}

2. C字符串和NSString之间的转换

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    //C字符串和NSString之间的转换
    char* cString = "Hello bill.";
    NSString *nsString = [NSString stringWithUTF8String:cString];
    NSLog(@"%@\r\n",nsString);
    
    char* cString2 = [nsString UTF8String];
    printf(cString2);
    printf("\r\n");

    [pool drain];
    return 0;
}

3.可变字符串

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSMutableString *mutableString = [NSMutableString stringWithString:@"可变字符串:"];
    
    [mutableString insertString:@"NSMutableString" atIndex:[mutableString length]];
    
    NSLog(@"%@", mutableString);

    [pool drain];
    return 0;
}

posted @ 2011-05-04 10:12  chenjunbiao  阅读(793)  评论(0编辑  收藏  举报