关闭

OC之对不可变字符串、可变字符串、可变数组的操作及OC中字符串的排序

标签: OC编程nsstring
348人阅读 评论(0) 收藏 举报
分类:


对不可变字符串的操作:
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);

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:32285次
    • 积分:837
    • 等级:
    • 排名:千里之外
    • 原创:52篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类