可变字符串
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//NSString是父类 可变字符串是子类
NSMutableString *mustr=[[NSMutableString alloc]init];
//将不可变的字符串放置到可变的字符串内
NSString *str=@"Welcome to oc";
mustr=[NSMutableString stringWithString:str];
//插入
[mustr insertString:@" student " atIndex:7];//在具体的位置插入
[mustr appendString:@" teacher "];//在末尾插入
NSLog(@"%@",mustr);
[mustr appendFormat:@"第二遍插入:%@",str];//格式化插入
NSLog(@"%@",mustr);
//删除
[mustr deleteCharactersInRange:NSMakeRange(8, 9)];//根据位置删除
NSLog(@"%@",mustr);
//查找并删除
NSRange rang=[mustr rangeOfString:@"第二遍插入: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);
NSMutableString *num=[[NSMutableString alloc]initWithString:@"233-343-123-000"];
NSRange rang2=[num rangeOfString:@"-"];
[num replaceCharactersInRange:rang2 withString:@""];
NSLog(@"%@",num);
NSString *newnum=[NSString string];
newnum=[num stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSLog(@"%@",newnum);
}
return 0;
}