IOS階段學習第14天筆記(NSString與NSMutableString),iosnsstring
IOS學習(OC語言)知識點整理
一、OC字串的操作
1)OC中字串分為兩種:
1、不可變字串NSString:不能修改對象內容,但是可以改變對象的指標。
2、可變字串NSMutableString:可以修改對象內容。
二、NSString 不可變字串的操作
1)將字串常量對象直接賦值給字串引用 NSString *str1=@"hello"; 字串對象的輸出格式: 1 NSLog(@"str1=%@",str1)。
2)initWithString可將OC中的字串對象構建字串引用 1 NSString *str2=[[NSString alloc]initWithString:str1];
3)initWithUTF8String可將C語言的字串建立OC的字串對象,將C字串轉換為OC字串:
1 NSString *str3=[[NSString alloc]initWithUTF8String:"iOS"];
4)initWithFormat可將OC的格式化字串建立OC的字串對象int age=20;
1 NSString *str4=[[NSString alloc]initWithFormat:@"name is %@,age is %d",str1,age];
5)可使用.length方法擷取字串的長度 1 NSUInteger len= str1.length;
6)characterAtIndex可根據下標取出某個字元 如:
NSString *str1=@"hello"; unichar c= [str1 characterAtIndex:0];//結果為:h
7)compare用於比較兩個字串,該方法區分大小寫, 返回結果為NSComparisonResult 枚舉類型資料 枚舉值有
1、NSOrderedAscending 表示前一個字串小於後一個字串
2、NSOrderedSame 表示兩個字串相等
3、NSOrderedDescending 表示前一個字串大於後一個字串
執行個體代碼:
NSString *str1=@"hello";NSString *str3=[[NSString alloc]initWithUTF8String:"iOS"];NSComparisonResult cmp=[str1 compare:str3];if(cmp==NSOrderedAscending){ NSLog(@"%@<%@",str1,str3);}else if (cmp==NSOrderedSame){ NSLog(@"%@=%@",str1,str3);}else if (cmp==NSOrderedDescending){ NSLog(@"%@>%@",str1,str3);}//結果:hello<iOS
8)caseInsensitiveCompare 不區分大小寫比較字串;比較字串,可以設定比較選項
NSNumericSearch:如果字串中有數字, 按數字大小比較,例如:ios5<iso12
NSCaseInsensitiveSearch:不區分大小寫比較,例如iOS7=ios7
執行個體代碼:
1 NSComparisonResult cmp=[str1 caseInsensitiveCompare:str3]; 2 str1=@"iOS7"; 3 str3=@"ios7"; 4 cmp=[str1 compare:str3 options:NSCaseInsensitiveSearch]; 5 if(cmp==NSOrderedAscending){ 6 NSLog(@"%@<%@",str1,str3); 7 }else if (cmp==NSOrderedSame){ 8 NSLog(@"%@=%@",str1,str3); 9 }else if (cmp==NSOrderedDescending){10 NSLog(@"%@>%@",str1,str3);11 } //結果:iOS7=ios7
9)isEqualToString 比較2個字串內容是否相等,返回BOOL(YES/NO)值
執行個體代碼:
1 NSString *str1=@"hello";2 NSString *str2=[[NSString alloc]initWithString:str1];3 if([str1 isEqualToString:str2]){4 NSLog(@"%@ equal %@",str1,str2);5 }//結果:hello equal hello
10)uppercaseString 將小寫字母字串轉為大寫 例如:
1 NSString *str1=@"hello";NSString *str6=[str1 uppercaseString]; 2 // 結果為:HELLO
11)lowercaseString 將大寫字母字串轉為小寫 。
12)hasPrefix 判斷是否以某個字串開頭(作為首碼)例如 :
NSString str1=@"www.baidu.com";if([str1 hasPrefix:@"www"]){ NSLog(@"yes");}// 結果:yes
13)hasSuffix 判斷是否以某個字串結尾(尾碼)例如:
1 NSString str1=@"www.baidu.com";2 if([str1 hasSuffix:@"com"]){3 NSLog(@"yes");4 }//結果:yes
14)substringFromIndex 從某個下標開始取子字串(到結束)例如:
1 NSString str1=@"This is string A"; 2 NSString *res=[str1 substringFromIndex:1]; //結果:his is string A
15)substringToIndex 從第一個字元開始取到某個下標的子字串,不包括當前數字對應的字元,取當前數字下標前面字元
例如:
1 NSString str1=@"This is string A";2 NSString *res=[str1 substringToIndex:2];//結果:Th3 [[str1 substringFromIndex:8] substringToIndex:5];//結果: strin
16)substringWithRange 根據範圍返回子字串
NSRange range={8,5};//直接給範圍值range=NSMakeRange(8, 5);//通過函數返回一個範圍的變數NSString str1=@"This is string A";NSString res=[str1 substringWithRange:range]; //結果:strin
17)rangeOfString 在字串中尋找子字串,返回這個字串的所在位置以及長度 NSRange
執行個體代碼:
1 NSString str1=@"This is string A";2 NSRange range=[str1 rangeOfString:@"string"]; //結果:{8,6}3 if(range.location!=NSNotFound){4 NSLog(@"find");5 }//結果:find
18)使用 #if 0 程式碼片段… #endif 可以讓系統跳過某段代碼不執行,例如:
#if 0 NSLog(@“hello world!”); #endifNSLog(@“我是第二段代碼!”);//執行結果:我是第二段代碼!
19)intValue、longValue、floatValue、doubleValue 可將字串類型的數字轉換為數實值型別的數字
例如: 1 NSString *str=@“88”; 2 int a=[str intValue];
20)NSString 多個字串的拼接
執行個體代碼:
1 NSString *str1=@"My name is";2 NSString *str2=@"John Zhang";3 NSString *str3=@"I am 45";4 NSString *s1=[NSString stringWithFormat:@"%@ %@ %@",str1,str2,str3];5 NSLog(@"s1=%@",s1); //結果:s1=My name is John Zhang I am 45
21)initWithContentsOfFile 將檔案內容讀取成字串對象, 第一個參數是檔案名稱(絕對路徑),第二個參數是編碼類別型,
第三個參數是錯誤回調資訊 例如:
1 NSString *content=[[NSString alloc]initWithContentsOfFile:"/Documents/ocfile/Person.h" encoding:NSUTF8StringEncoding error:nil];
22)直接列印對象,預設調用description方法,顯示對象的類名和地址,可以重寫description方法,
注意:如果直接輸出類對象中含有中文內容會有亂碼,所以含中文內容的類對象必須遍曆輸出。
三、NSMutableString 可變字串的操作
1)initWithString 用不可變字串建立可變字串對象 例如:
1 NSString *str=@"This is string B"; 2 NSMutableString *mStr=[[NSMutableString alloc]initWithString:str];
2)insertString 在指定下標位置插入一個字串 例如:
1 NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];2 [mStr insertString:@"hello " atIndex:0]; //結果:hello This is string B
3)appendString 在字串後面追加字串 例如:
1 NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];2 [mStr appendString:@" shanghai”];//結果:This is string B shanghai
4)appendFormat 在字串後面追加一個格式化字串 例如:
1 NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];2 [mStr appendFormat:@" %d",20];//結果:This is string B 20
5)deleteCharactersInRange 將指定範圍的字串刪除 例如:
1 NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];2 [mStr deleteCharactersInRange:NSMakeRange(0, 6)];// 結果: s string B
6)replaceCharactersInRange 將指定範圍的字串用新的字串替換 例如:
1 NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];2 [mStr replaceCharactersInRange:NSMakeRange(2, 2) withString:@"IS"]; //結果:ThIS is string B3 //將字串中所有的is替換為IS4 NSRange range=[mStr rangeOfString:@"is"];5 while (range.location!=NSNotFound) {6 [mStr replaceCharactersInRange:range withString:@"IS"];7 range=[mStr rangeOfString:@"is"];8 }
7)replaceOccurrencesOfString 將字串中指定範圍內所有的is替換成IS 例如:
1 NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];2 [mStr replaceOccurrencesOfString:@“is” withString:@“IS” options:1 range:NSMakeRange(0, mStr.length)]; 3 //結果:ThIS IS string B
8)setString 修改字串內容 例如:
1 NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];2 [mStr setString:@"hello"]; //結果:hello
9)NSMutableString 實現多個字串的拼接
執行個體代碼:
1 NSString *str1=@"My name is";2 NSString *str2=@"John Zhang";3 NSString *str3=@"I am 45";4 NSMutableString *s2=[[NSMutableString alloc]initWithString:str1]; 5 6 [s2 appendString:str2];7 [s2 appendString:@" "];8 [s2 appendString:str3];9 NSLog(@"s2=%@",s2); //結果:s2=My name isJohn Zhang I am 45