//字符串的连接 #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString* str1 = @"Hello"; NSString* str2 = @" World"; //str1和str2都没有变,方法返回一个新的字符串 NSString* str3 = [str1 stringByAppendingString:str2]; NSLog(@"%@", str3); //格式化输出,灵活性强 str3 = [str1 stringByAppendingFormat:@"%@!1+1=%d",str2,1+1]; NSLog(@"%@", str3); [pool drain]; return 0; } //可变字符串的操作 #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; //NSMutableString是NSString的子类 NSMutableString* str1 = [[NSMutableString alloc] initWithCapacity:0]; //在已有字符串后面添加新的字符串 [str1 appendString:@"Hello world!good"]; //根据范围删除字符串 NSRange range = {12, 4}; [str1 deleteCharactersInRange:range]; //在指定的位置后面插入字符串 [str1 insertString:@"Hi!" atIndex:0]; NSLog(@"%@", str1); [str1 setString:@"Hello, iOS"]; // 将已有的字符串换成其它的字符串 NSLog(@"%@", str1); [str1 replaceCharactersInRange:NSMakeRange(7, 3) withString:@"Apple"]; NSLog(@"%@", str1); [str1 release]; [pool drain]; return 0;