//
// main.m
// 可变的字符串
//
// Created by 博博 on 16/1/7.
// Copyright (c) 2016年 com.bb. All rights reserved.
//
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//nsstring父类 可变字符串子类
NSMutableString *mustr=[[NSMutableString alloc]init];
NSMutableString *mustr1=[NSMutableString stringWithFormat:@"hello"];
NSLog(@"str=%@",mustr1);
NSString *str=@"welcome to oc";
mustr=[NSMutableString stringWithString:str];
NSLog(@"%@",mustr);
//插入
[mustr insertString:@" student" atIndex:7];
NSLog(@"%@",mustr);
[mustr appendString:@" teacher"];//插入末尾
NSLog(@"%@",mustr);
[mustr appendFormat:@"第二遍:%@",str];
NSLog(@"%@",mustr);
//删除
[mustr deleteCharactersInRange:NSMakeRange(8, 9)];
NSLog(@"%@",mustr);
//查找并删除
NSRange rang=[mustr rangeOfString:@"teacher第二遍:welcome to oc"];
if(rang.location!=NSNotFound){
[mustr deleteCharactersInRange:rang];
}
NSLog(@"%@",mustr);
//重新赋值
[mustr setString:str];
NSLog(@"%@",mustr);
//替换
NSRange rang1=[mustr rangeOfString:@"oc"];
[mustr replaceCharactersInRange:rang1 withString:@"ios"];
NSLog(@"%@",mustr);
}
return 0;
}