Foundation--NSMutableString

NSMutableString是NSString的子类,所以可以在NSMutableString中使用NSString的所有功能。由于我们“无偿”获得了比NSString更多的功能和特性,所以在任何可以使用NSString的地方都可以使用NSMutableString来代替。

在已有字符串后面添加字符

NSMutableString *mutableString = [NSMutableString stringWithString:@"This is a mutableString."];

[mutableString appendString:@"Added String."];

//This is a mutableString.Added String.

[mutableString appendFormat:[NSString stringWithFormat:@"Added String."]];

//This is a mutableString.Added String.Added String.

 在已知字符串中按照所给出的长度和范围删除字符

NSMutableString *mutableString = [NSMutableString stringWithString:@"This is a mutableString."];

[mutableString deleteCharactersInRange:NSMakeRange(0, 5)];

//is a mutableString. 

一般情况下,会是下面两个方法一起使用

    - (void)deleteCharactersInRange:(NSRange)range;
    - (NSRange)rangeOfString:(NSString *)aString;

在已知字符串的指定位置插入给出的字符串

NSMutableString *mutableString = [NSMutableString stringWithString:@"This is a mutableString."];

[mutableString insertString:@"Hi,"atIndex:0];

//Hi,This is a mutableString. 

将已知的字符串替换成其他字符串

NSMutableString *mutableString = [NSMutableString stringWithString:@"This is a mutableString."];

[mutableString setString:@"Hello MutableString."];

//Hello MutableString. 

按照所给出范围和字符串替换原有的字符串

NSMutableString *mutableString = [NSMutableString stringWithString:@"This is a mutableString."];

[mutableString replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];

//That is a mutableString.

你可能感兴趣的:(ios,Foundation)