NSString以及NSArray的习题练习

本文介绍了使用Objective-C进行字符串处理的方法,包括字符串的替换、截取和格式化,以及如何将不同类型的变量存储到NSMutableArray中。此外,还提供了一个简单的通讯录管理示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    // 作业1
    // 将“⽂艺⻘年”改成“213青年”。
    // 不可变字符串
    NSString *str1 = @"文艺青年";
    // 可变字符串
    NSMutableString *str2 = [NSMutableString stringWithString:@"文艺青年"];
    // 部分替换
    NSLog(@"%@", [str1 stringByReplacingCharactersInRange:NSMakeRange(0, 2) withString:@"213"]);
    [str2 replaceCharactersInRange:NSMakeRange(0, 2) withString:@"213"];
    NSLog(@"%@", str2);
    NSLog(@"%@", [str1 stringByReplacingOccurrencesOfString:@"文艺" withString:@"213"]);
    // 整体替换
    [str2 setString:@"213青年"];
    NSLog(@"%@", str2);
    // 剪切+插入
    NSMutableString *str3 = [NSMutableString stringWithString:[str1 substringFromIndex:2]];
    NSMutableString *str4 = [NSMutableString stringWithString:[str1 substringWithRange:NSMakeRange(2, 2)]];
    NSLog(@"%@", str3);
    [str4 insertString:@"213" atIndex:0];
    NSLog(@"%@", str4);
    // 设置一个213拼接青年
    // 略
    // 将整数123转换为字符串“123”。
    NSInteger num = 123;
    NSString *str0 = [NSString stringWithFormat:@"%ld", num];
    NSLog(@"%@", str0);
    // 将 “i love you”单词⾸首字⺟母变⼤大写 “I love You”
    NSString *str5 = @"i love You";
    NSLog(@"%@", str5.capitalizedString);
    NSLog(@"%@", [str5.capitalizedString stringByReplacingCharactersInRange:NSMakeRange(2, 1) withString:@"l"]);
    NSLog(@"%@", [str5.capitalizedString stringByReplacingOccurrencesOfString:@"L" withString:@"l"]);
    // 截取下列字符串中"|"前面和后面的字符串,并输出.
    NSMutableString *str = [NSMutableString stringWithString:@"20|http: //www.baidu.com"];
    NSLog(@"%@", [str componentsSeparatedByString:@"|"]);
    NSArray *arr1 = [str componentsSeparatedByString:@"|"];
    NSLog(@"%@",[arr1 objectAtIndex:0]);
    NSLog(@"%@",[arr1 objectAtIndex:1]);
    // 作业2
    // 往可变数组里存储int,float,double,BOOL,NSRange等类型数据.提示:将标量转换为NSNumber或NSValue才能进行存储
    int a = 1;
    float b = 1.1;
    double c = 1.11;
    BOOL d = 0;
    NSRange range = {0,1};  // range前不能带*    // why?
    NSNumber *number1 = [NSNumber numberWithInt:a];
    NSNumber *number2 = [NSNumber numberWithFloat:b];
    NSNumber *number3 = [NSNumber numberWithDouble:c];
    NSNumber *number4 = [NSNumber numberWithBool:d];
    NSValue *value = [NSValue valueWithRange:range];
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:number1, number2, number3, number4, value, nil];
    for (NSInteger i = 0; i < arr.count; i++) {
        NSLog(@"%@", arr[i]);
        NSLog(@"%@", [arr objectAtIndex:i]);
    }
    // 想要变回原来的类型就用intValue,doubleValue等
    // 作业3
    // 低等难度通讯录
    // 封装的概念 学了之后把它加上
    Contact *p1 = [[Contact alloc] initWithName:@"ShiChuanYang" number:@"13945873691" sex:@"男" address:@"呼玛县" groupName:@"朋友" age:25];
    Contact *p2 = [[Contact alloc] initWithName:@"WangJunMin" number:@"13245678903" sex:@"男" address:@"鹤岗市" groupName:@"朋友" age:23];
    Contact *p3 = [[Contact alloc] initWithName:@"WangErMaZi" number:@"13222222597" sex:@"男" address:@"浩良河" groupName:@"陌生人" age:28];
    Contact *p4 = [[Contact alloc] initWithName:@"FanCong" number:@"13945870377" sex:@"男" address:@"甘肃省" groupName:@"黑名单" age:24];
    Contact *p5 = [[Contact alloc] initWithName:@"LiMuRan" number:@"13945117673" sex:@"女" address:@"宁夏" groupName:@"朋友" age:24];
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:p1,p2,p3,p4, nil]; 
    if ([p5.name isEqual:@""] || [p5.number isEqual:@""]) {
        NSLog(@"添加失败");
    } else {
        [arr addObject:p5];
        for (Contact *temp in arr) {
            [temp sayHi];
        }
    }
    NSLog(@"**********");
    for (Contact *temp in arr) {
        if ([temp.groupName  isEqual: @"朋友"]) {
            [temp sayHi];
        }
    }
    NSLog(@"**********");
    for (Contact *temp in arr) {
        if ([temp.number  isEqual: @"13945117673"]) {
            [temp sayHi];
        }
    }
    NSLog(@"**********");
    for (Contact *temp in arr) {
        if ([temp.sex  isEqual: @"女"]) {
            [temp sayHi];
        }
    }
    NSLog(@"**********");
    // 在forin循环中如果对数组进行修改会导致崩溃,因为如果修改,会影响循环性,导致数组越界访问等问题,加一个break看起来貌似管用(-_-'汗);
    // 这里更准确的说法是一个forin不能同时对一个数组进行遍历和修改,应该再设置一个数组保存原数组的数据,这样才能同时对新数组进行修改,或者写两个forin?
    for (Contact *temp in arr) {
        if ([temp.name  isEqual: @"FanCong"]) {
            [arr removeObject:temp];
            break;
        }
    }
//    [arr removeObject:p4];
    for (Contact *temp in arr) {
        [temp sayHi];
    }
    NSLog(@"**********");
    for (Contact *temp in arr) {
        if ([temp.groupName isEqual:@"陌生人"]) {
            [arr removeObject:temp];
            break;
        }
    }
    for (Contact *temp in arr) {
        [temp sayHi];
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值