NSMutableString的總結

一.NSMutableString的基本概念app

1.NSMutableString是可變字符串,是NSString的子類code

2.NSString是不可變的,裏面的文字內容是不能修改的orm

3.NSMutableString是可變的,裏面的內容能夠隨時修改字符串

4.NSMutableString可使用NSString的全部方法string

二.NSMutableString的初始化it

//1.NSString的初始化方式均可以用在NSMulString
NSMutableString *str = [NSMutableString stringWithFormat:@"%d",233];
NSMutableString *str2 = [NSMutableString stringWithUTF8String:"2333"];
        
//2.空字符的初始化
NSString *str3 = @"123456789";
//可變的空字符串
NSMutableString *str4 = [NSMutableString string];
        
//3.不可變字符串轉可變
NSMutableString *str5 = [NSMutableString stringWithString:str3];
//徹底等同
NSMutableString *str6 = [str3 mutableCopy];

三.拼接字符串 - appendStringtable

//初始化NSMutableString
NSMutableString *str = [NSMutableString string];      

NSString *st = @"asfas";
//拼接字符串  
[str appendString:[NSString stringWithFormat:@"%@",st]];
//拼接一段格式化字符串到最後
[str2 appendFormat:@"%@",st];

四.刪除range範圍內的字符串 - deleteCharactersInRangeclass

NSMutableString *str = [NSMutableString stringWithFormat:@"%d",asfasfa];
//刪除方法
[str deleteCharactersInRange:NSMakeRange(0, 5)];
//輸出:str - dele -fa 
NSLog(@"str - dele - %@",str);

五.插入字符串object

NSMutableString *str = [NSMutableString stringWithFormat:@"%d",asfasfa];
//把字符串01234插入到字符串str第0位
[str insertString:@"01234" atIndex:0];
//輸出:str - insertString - 01234asfasfa
NSLog(@"str - insertString - %@",str2);

六.替換字符串方法

NSMutableString *str3 = [@"www.baidu.com" mutableCopy];
//把str3第4位,長度爲5的字符串替換爲YaHoo
[str3 replaceCharactersInRange:NSMakeRange(4, 5) withString:@"Yahoo!"];
//輸出:str3 - www.YaHoo.com
NSLog(@"str3 - %@",str3);
相關文章
相關標籤/搜索