- NSMutableString是NSString的子類,即NSString有的方法NSMutableString也有。
- NSMutableString是可變的字符串,NSString是不可變字符串,即NSMutableString可以對字符串進行增刪改操作,NSString則不可以。
NSString不可變字符串的使用:
1、創(chuàng)建字符串對象
NSString *str1= [NSString stringWithString:@"hello world1"]; //一般不用這種方法
NSLog(@"=====str1:%@",str1);
NSString *str2 = [[NSString alloc] initWithString:@"hello world2"];//一般也不用這種方法
NSLog(@"=====str2:%@",str2);
NSString *str3 = @"hello world3"; //常用方法
NSLog(@"=====str3:%@",str3);
//如果事先不知道字符串的值,可以先初始化一個空字符串,后面再進行賦值
NSString *str4 = nil;
str4 = @"hello world4";
NSLog(@"=====str4:%@",str4);
2、C類型字符串轉化成OC類型字符串,OC類型轉化成C類型
char *s = "hello world";
//參數一 常量字符串 參數二 編碼格式
NSString *str = [[NSString alloc] initWithCString:s encoding:NSUTF8StringEncoding];
NSLog(@"=====str1:%@",str1);
NSString *str1 = @"This is oc";
const char *p = [str1 UTF8String];
NSLog(@"=====p:%s",p);
3、字符串轉化成基本數據類型
NSString *str = @"100";
NSInteger i = str.integerValue; //100
NSString *str2 = @"100.1";
NSInteger f = str2.floatValue;//1oo.1
NSString *str3 = @"1";
BOOL b = str3.boolValue; //YES
4、字符串格式化
NSString *str1 = [NSString stringWithFormat:@"%@%d%@,體重%.1fkg",@"張三身高",180,@"cm",60.5];
//打印結果:======str1張三身高180cm,體重60.5kg
NSLog(@"======str1:%@",str1);
NSString *name = @"張三";
NSInteger age = 20;
NSString *str88 = [NSString stringWithFormat:@"%@的年齡=%d",name,age];
5、字符串長度
NSString *str = @"hello";
NSLog(@"=====length:%d 或者 length:%d",str.length,[str length]);
6、比較2個字符串是否相等
NSString *str1 = @"hello";
NSString *str2 = @"hello2";
if ([str1 isEqualToString:str2]) {
NSLog(@"相等");
}else{
NSLog(@"不相等");
}
7、比較2個字符串的大小
// 比較結果 NSOrderedSame 相等 NSOrderedAscending 小于 NSOrderedDescending 大于
NSString *str1 = @"student1";
NSString *str2 = @"student2";
NSInteger result = [str1 compare:str2];
if (result == NSOrderedSame) {
NSLog(@"等于");
} else if(result == NSOrderedAscending){
NSLog(@"小于");
}else {
NSLog(@"大于");
}
8、字符串查找
//字符串查找,在str1中能否查找到str2和str3字符串
NSString *str1 = @"NSOrderedSame,NSOrderedAscending,NSOrderedDescending";
NSString *str2 = @"Same";
NSString *str3 = @"Same2";
NSRange range = [str1 rangeOfString:str2];
NSRange range2 = [str1 rangeOfString:str3];
//判斷條件
if (range.location == NSNotFound && range.length == 0) {
NSLog(@"===1:沒有找到的判斷");
} else {
NSLog(@"===1:location is %ld length is %ld",range.location,range.length);
}
if (range2.location == NSNotFound && range2.length == 0) {
NSLog(@"===2:沒有找到的判斷");
} else {
NSLog(@"===2:location is %ld length is %ld",range2.location,range2.length);
}
//最終輸出
//===1:location is 9 length is 4
//===2:沒有找到的判斷
9、字符串截取
NSString * str1 = @"張三2010年從北京大學畢業(yè)";
// 從指定的index開始截取到字符串末尾
NSString * str2 = [str1 substringFromIndex:str1.length-3];
NSLog(@"str2 is %@",str2);//str2 is 學畢業(yè)
//從開始位置一直截取到指定的index位置
NSString *str3 = [str1 substringToIndex:2];
NSLog(@"str3 is %@",str3);//str3 is 張三
// 截取第三到第八之間的字符(包含第三和第八),注意oc的下標是從0開始計數
NSString *str4 = [str1 substringWithRange:NSMakeRange(3, 6)];
NSLog(@"str4 is %@",str4);//str4 is 010年從北
NSMutableString可變字符串的使用
NSMutableString是NSString的子類,所以上面NSString的所有方法NSMutableString都可以使用,下面我們著重講解下可變字符串才擁有的增刪改方法
1、創(chuàng)建可變字符串
NSMutableString *str = [NSMutableString stringWithString:@"hello"];//靜態(tài)方法
NSMutableString *str1 = [[NSMutableString alloc] initWithString:@"hello"];//動態(tài)方法
NSMutableString *str2 = @"hello";//通過這種方法創(chuàng)建的對象是個不可變字符串,使用可變字符串的方法會crash,所以可變字符串一般不使用這種方法創(chuàng)建
2、可變字符串的增刪改方法
NSMutableString *str = [NSMutableString stringWithString:@"hello"];
//追加 world
[str appendString:@"world"];
NSLog(@"str is:%@",str); //str is:helloworld
//插入 你好
[str insertString:@"你好" atIndex:5];//str is:hello你好world
NSLog(@"str is:%@",str);
//刪除 你好
// [str deleteCharactersInRange:NSMakeRange(5, 2)]; 或者
[str deleteCharactersInRange:[str rangeOfString:@"你好"]];
NSLog(@"str is:%@",str); //str is:helloworld
NSString與NSMutableString的區(qū)別
1、不可變字符串先后操作的內存空間不一樣 而可變字符串是一致的
NSMutableString *str = [NSMutableString stringWithString:@"hello"];
NSString *str2 = @"hello";
NSLog(@"before str:%@ %p",str,str);
NSLog(@"before str2:%@ %p",str2,str2);
[str appendString:@"world"];
str2 = @"helloworld";
NSLog(@"after str:%@ %p",str,str);
NSLog(@"after str2:%@ %p",str2,str2);
/*
輸出結果:
before str:hello 0x103314420
before str2:hello 0x100005220
after str:helloworld 0x103314420
after str2:helloworld 0x1000052a0
從輸出結果可以看出,不可變字符串先后操作的內存空間不一樣 而可變字符串是一致的
*/
2、深拷貝(mutableCopy)和淺拷貝(copy)的區(qū)別
先看NSString的深淺拷貝
NSString * string = @"hello world";
//淺拷貝
NSString * copyString = [string copy];
//深拷貝
NSMutableString * mutableCopyString = [string mutableCopy];
NSLog(@"\nstring = %p\ncopyString = %p\nmutableCopyString = %p",string,copyString,mutableCopyString);
/*
輸出結果:
string = 0x100005220
copyString = 0x100005220
mutableCopyString = 0x10072dcb0
*/
由此看出NSString中
淺拷貝:未產生新對象
深拷貝:產生新對象
再看NSSMutableString的深淺拷貝
NSMutableString * string = [NSMutableString stringWithString:@"hello world"];
//淺拷貝
NSString * copyMString = [string copy];
//深拷貝
NSMutableString * mutableCopyMString = [string mutableCopy];
NSLog(@"\nmString = %p\ncopyMString = %p\nmutableCopyMString = %p",string,copyMString,mutableCopyMString);
/*
輸出結果:
mString = 0x102900150
copyMString = 0x102900180
mutableCopyMString = 0x1029001a0
*/
由此看出NSSMutableString中
淺拷貝:產生新對象
深拷貝:產生新對象