NSMutableString的几个常用接口

来源:互联网 发布:淘宝自由搭配套餐 功能 编辑:程序博客网 时间:2024/05/16 06:22

不同于固定字符串类型NSString,NSMutableString的可修改属性,适应性更好。

NSMutableString继承自NSString,因此有的接口,比如初始化接口可以使用NSString的格式。另外,

NSMutableString的其他常用接口还有:

- (void)appendString:(NSString *)aString

- (void)appendFormat:(NSString *)format...

- (void)deleteCharactersInRange:(NSRange)aRange

- (void)insertString:(NSString *)aString atIndex:(NSUInteger)anIndex

- (void)replaceCharactersInRange:(NSRange)aRange withString:(NSString *)aString

- (void)setString:(NSString *)aString


举例如下:

        NSString *str1 = @"hello world.";        NSMutableString *str2 = [[NSMutableString alloc] initWithString:str1];        NSMutableString *str3 = [NSMutableString stringWithString:str1];        NSMutableString *str4 = [[NSMutableString alloc] initWithFormat:@"he%@ wor%cd.",@"llo",'l'];        NSMutableString *str5 = [NSMutableString stringWithFormat:@"he%@ wor%cd.",@"llo",'l'];                NSLog(@"str2 is %@", str2);        NSLog(@"str3 is %@", str3);        NSLog(@"str4 is %@", str4);        NSLog(@"str5 is %@", str5);                [str2 setString: @"str2 is a mutable string."];        NSLog(@"str2 is %@", str2);        [str3 appendString:@"str3 is a mutable string."];        NSLog(@"str3 is %@", str3);        [str4 appendFormat:@"str4 is a %@", @"mutable string."];        NSLog(@"str4 is %@", str4);        [str5 insertString:@"###" atIndex:7];        NSLog(@"str5 is %@", str5);        [str5 deleteCharactersInRange:NSMakeRange(2, 3)];        NSLog(@"str5 is %@", str5);        [str5 replaceCharactersInRange:NSMakeRange(6, 2) withString: @"****"];        NSLog(@"str5 is %@", str5);

输出结果:

2015-11-02 22:35:36.233 testNSMutableString[549:16998] str2 is hello world.2015-11-02 22:35:36.233 testNSMutableString[549:16998] str3 is hello world.2015-11-02 22:35:36.234 testNSMutableString[549:16998] str4 is hello world.2015-11-02 22:35:36.234 testNSMutableString[549:16998] str5 is hello world.2015-11-02 22:35:36.234 testNSMutableString[549:16998] str2 is str2 is a mutable string.2015-11-02 22:35:36.234 testNSMutableString[549:16998] str3 is hello world.str3 is a mutable string.2015-11-02 22:35:36.234 testNSMutableString[549:16998] str4 is hello world.str4 is a mutable string.2015-11-02 22:35:36.234 testNSMutableString[549:16998] str5 is hello w###orld.2015-11-02 22:35:36.234 testNSMutableString[549:16998] str5 is he w###orld.2015-11-02 22:35:36.234 testNSMutableString[549:16998] str5 is he w##****rld.

0 0
原创粉丝点击