- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- //格式化延展
- //appendFormat
- NSMutableString *str1=[NSMutableString string];
- [str1 appendFormat:@"jpg%d",10];
- //删除range.location处range.lenth长度字符串
- //-(void)deleteCharactersInRange:(NSRange)range;
- NSMutableString *str2=[NSMutableString stringWithFormat:@"Lazy&jiangkeshanshan"];
- NSRange range=[str2 rangeOfString:@"jiangke"];
- [str2 deleteCharactersInRange:range];
- NSLog(@"%@",str2);
- //在location位置插入一个字符串
- //-(void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;
- NSMutableString *str3=[NSMutableString stringWithFormat:@"lazy&"];
- NSRange range1=[str3 rangeOfString:@"&"];
- [str3 insertString:@"shanshan" atIndex:range1.location+1];
- NSLog(@"%@",str3);
- //使用aString替换range范围内字符串
- //-(void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString
- NSMutableString *str4=[NSMutableString stringWithFormat:@"lazy&miandian"];
- NSRange range2=[str4 rangeOfString:@"miandian"];
- [str4 replaceCharactersInRange:range2 withString:@"shanshan"];
- NSLog(@"%@",str4);
- //错误1:给可变字符串赋值了不可变内容
- // NSMutableString *str5=@"adb";
- //错误2:给str的string属性设为空
- // NSMutableString *str5=@"abc";
- // str5.string=@"";
- }
- return 0;
- }
复制代码 |
|