1. 程式人生 > >OC之對不可變字串、可變字串、可變陣列的操作及OC中字串的排序

OC之對不可變字串、可變字串、可變陣列的操作及OC中字串的排序



對不可變字串的操作:
NSString*str = [[NSStringalloc]initWithString:@"^封梟^"]; NSLog(@"str = %@",str); NSString*str1 = [NSStringstringWithString:@"-若雪-"]; NSLog(@"str1 = %@",str1); //求字串長度.length NSUIntegerlength  = str1.length; NSLog(@"lenth = %lu",length); //字串拼接 NSString*str2 = [str1 stringByAppendingString
:str];
NSLog(@"str2 = %@",str2); //多個字串的拼接 NSString*str3 = [str stringByAppendingFormat:@"%@%@%@",str1,str,str2]; NSLog(@"str3 = %@",str3); //字串替換 NSString*str4 = [str stringByReplacingOccurrencesOfString:strwithString:str1]; NSLog(@"str4 = %@",str4); //在(xy)範圍替換字串 NSString*str5 = [str1 stringByReplacingCharactersInRange
:NSMakeRange(1,3)withString:@"微笑"];
NSLog(@"str5 = %@",str5); //字串替換為@“xx” NSString*str6 = [str stringByReplacingOccurrencesOfString:strwithString:@"後會無期"]; NSLog(@"str6 = %@",str6); 對可變字串的操作: NSMutableString*m = [[NSMutableStringalloc]initWithString:@"若你安好…"]; //拼接 [mappendString:@"便是晴天···"]; NSLog
(@"%@",m);
//插入字元 [minsertString:@"微微一笑很傾城"atIndex:12]; NSLog(@"%@",m); //刪除字串 [mdeleteCharactersInRange:NSMakeRange(0,1)]; NSLog(@"%@",m); [mdeleteCharactersInRange:NSMakeRange(0,0)]; NSLog(@"%@",m); NSString是不可改變字串,創建出來後不能改變長度和內容 NSMutablleString是可變字串,建立之後可以修改內容 可變(NSMutableString)和不可變(NSString) 不可變字串的修改操作,本質是得到一個新的字串。不可變字串的修改是以string開頭,有返回值。 可變字串的修改方法沒有返回值,修改原字串。 //不可變字串的修改 NSString*result1 = @"y.png"; NSString*newresult = [NSStringstring]; if([result1hasSuffix:@"png"]) { newresult = [result1stringByReplacingOccurrencesOfString:@"png"withString:@"jpg"]; }else{ newresult = [result1stringByAppendingString:@".jpg"]; } NSLog(@"newresult = %@",newresult); //可變字串的修改 NSString*n = [[NSMutableStringalloc]initWithString:@"k.png"]; if([nhasSuffix:@"k.png"]) { n = [nstringByReplacingOccurrencesOfString:@"png"withString:@"jpg"]; }else{ n = [nstringByAppendingString:@".jpg"]; } NSLog(@"n = %@",n);
字串排序:用compare比較 if([[s1 name]compare: [s2name]] <0) { returnNSOrderedAscending;    }elseif([[s1 name]compare: [s2name]] >0){ returnNSOrderedDescending;     }else{ returnNSOrderedSame;   } 對可變陣列的操作 NSMutableArray*ma = [[NSMutableArrayalloc]initWithObjects:@"她說",@"陰天",@"明天,你好",@"離歌",nil]; [maaddObject:@""]; for(inti = 0; i < array.count; i++) { NSLog(@"%@",array[i]); } //插入 [mainsertObject:@"日不落"atIndex:3]; //刪除 [maremoveObjectAtIndex:3]; for(inti = 0; i < ma.count; i++) { NSLog(@" 3%@",ma[i]); } //替換 [mareplaceObjectAtIndex:4withObject:@"天亮了"]; for(inti = 0; i < ma.count; i++) { NSLog(@" 4%@",ma[i]); } //交換指定位置的元素 [maexchangeObjectAtIndex:2withObjectAtIndex:1]; for(inti = 0; i < ma.count; i++) { NSLog(@" 5%@",ma[i]); } //int型別轉換為NSNumber物件型別存入陣列 NSNumber*o1 = [NSNumbernumberWithInt:123]; NSNumber*o2 = [NSNumbernumberWithInt:234]; //float轉換為NSNumber物件存入陣列 NSNumber*o3 = [NSNumbernumberWithFloat:'A']; NSNumber*o4 = [NSNumbernumberWithFloat:'Y']; //把陣列元素取出來,用xxxValue在轉換為原來型別 NSArray*a2 = [NSArrayarrayWithObjects:o1,o2,o3,o4,nil]; intw = [[a2 objectAtIndex:0]intValue]; NSLog(@"a2 = %d",w); floatb = [[a2 objectAtIndex:3]intValue]; NSLog(@"a2 = %.fd",b);