rosylxf

驾驭命运的舵是奋斗。不抱有一丝幻想,不放弃一点机会,不停止一日努力!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

NSMutableString与字符串的连接

Posted on 2012-07-01 22:51  rosylxf  阅读(412)  评论(0)    收藏  举报
 1 //字符串的连接
 2 #import <Foundation/Foundation.h>
 3 
 4 int main (int argc, const char * argv[]) {
 5     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 6     
 7     NSString* str1 = @"Hello";
 8     NSString* str2 = @" World";
 9     //str1和str2都没有变,方法返回一个新的字符串
10     NSString* str3 = [str1 stringByAppendingString:str2];
11     NSLog(@"%@", str3);
12     
13     //格式化输出,灵活性强
14     str3 = [str1 stringByAppendingFormat:@"%@!1+1=%d",str2,1+1];
15     NSLog(@"%@", str3);
16     
17     [pool drain];
18     return 0;
19 }
20 
21 //可变字符串的操作
22 #import <Foundation/Foundation.h>
23 
24 int main (int argc, const char * argv[]) {
25     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
26     
27     //NSMutableString是NSString的子类
28     NSMutableString* str1 = [[NSMutableString alloc] initWithCapacity:0];
29     //在已有字符串后面添加新的字符串
30     [str1 appendString:@"Hello world!good"];
31     //根据范围删除字符串
32     NSRange range = {12, 4};
33     [str1 deleteCharactersInRange:range];
34     //在指定的位置后面插入字符串
35     [str1 insertString:@"Hi!" atIndex:0];
36     NSLog(@"%@", str1);
37     
38     [str1 setString:@"Hello, iOS"]; // 将已有的字符串换成其它的字符串
39     NSLog(@"%@", str1);
40     
41     [str1 replaceCharactersInRange:NSMakeRange(7, 3) withString:@"Apple"];
42     NSLog(@"%@", str1);
43     
44     
45     [str1 release];
46     [pool drain];
47     return 0;