oc中可变数组和可变字符串相关操作

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        // insert code here...

        //不可变字符串

        NSString* str=@"hello";

        NSString* str1=@" world";

        //可变字符串初始化

        NSMutableString* mstr=[[NSMutableString alloc] initWithCapacity:10];

        //插入

        [mstr insertString:@"http://" atIndex:0];

        //第一个参数:即将要插入的字符串   第二个参数:插入位置

        

        [mstr insertString:@"www.baidu.com" atIndex:[mstr length]];

        

        //追加

        [mstr appendFormat:@"?username=%@", @"zhang"];

        

        //删除

        [mstr deleteCharactersInRange:NSMakeRange(20, 15)];//所要删除的内容的范围

        //修改

        [mstr setString:@"aabb"];//参数新的内容

        //替换

        [mstr replaceCharactersInRange:NSMakeRange(1, 2) withString:@"cc"];

        //第一个参数:指定要替换的范围 第二个参数:新的内容

        

        [mstr replaceOccurrencesOfString:@"a" withString:@"0" options:0 range:NSMakeRange(0, mstr.length)];

        //第一个参数:即将替换的内容 第二个参数:新内容 第三个参数:查找方式    第四个参数:查找范围

        //功能:将查找范围内所有匹配的子串全部替换为新内容

        

        //字符串分隔

        [mstr setString:@"https://www.daidu.com?username=zhang"];

        NSArray* array=[mstr componentsSeparatedByString:@"?"];

        NSLog(@"array:%@", array);

        NSString* component=array[1];

        NSLog(@"%@", component);

        NSArray* array1=[mstr componentsSeparatedByString:@"&"];

        

        //NSLog(@"array:%@", array1);

        

       // NSLog(@"%@", mstr);

        

        NSArray* array2=[NSArray arrayWithObjects:@"张三", @"李四", @"王二", nil];

        for(int i=0; i<[array1 count]; i++)

        {

            //[array1 count]:获取数组的元素个数

            NSLog(@"%@", array1[i]);//array[i]  :表示数组中下标为i的元素

            //NSLog(@"%@", [array1 objectAtIndex:i]);

            //获得下标为i的元素。与上述语句相等

        }

        //快速遍历 从集合array1中第一个元素开始遍历,每次都将便利到的元素赋值给object暂时管理,然后执行循环体。

        for(id object in array1)

        {

            NSLog(@"%@", object);

        }

        NSLog(@"%@", array2);

        //获得指定对象的位置

        NSUInteger index=[array2 indexOfObject:@"张三"];

        if (index==NSNotFound) { //待查找的元素

            NSLog(@"element is not in array");

        }

        else

        {

            NSLog(@"%lu", index);//打印待查找元素的下标

        }

        

        //可变数组初始化

        NSMutableArray* marray=[[NSMutableArray alloc] initWithCapacity:4];

        NSMutableArray* marray1=[NSMutableArray arrayWithCapacity:4];

        //增 删 交换

        [marray addObject:@"hello"];//参数:即将加入集合的元素,默认将元素添加到集合的末尾

        [marray insertObject:@"world" atIndex:0];

        //参数一:即将插入的对象   参数二:位置(下标)

        NSLog(@"%@", marray);//向数组中添加元素的时候,只能插入非空对象。

        [marray addObject:@""];

        //[marray addObject:nil];  //向数组中插入空对象是,系统报错

        NSLog(@"%@", marray);

        [marray removeObject:@""];//删除指定的对象

        NSLog(@"%@", marray);

        [marray removeObjectAtIndex:0];//删除指定位置的元素(注意:下标不能越界, 否则程序崩溃)

        NSLog(@"%@", marray);

        

        [marray addObject:@"iphone"];

        [marray addObject:@"ipod"];

        NSLog(@"%@", marray);

        [marray removeLastObject];//删除最后一个元素

        NSLog(@"%@", marray);

        [marray addObject:@"ipod01"];

        [marray exchangeObjectAtIndex:0 withObjectAtIndex:2];

        NSLog(@"%@", marray);

//        NSArray* sortedArray=[marray sortedArrayUsingSelector:@selector(compare:)];

//        //receiver既可以是可变数组, 也可以是不可变数组,receiver的内容不会受到任何影响。将排序完成的内容存入一个新数组中返回. 使用系统的compare:进行数组元素的两两比较,最终的数列是按从小到大的顺序进行排列。

//        NSLog(@"%@", sortedArray);

        [marray sortUsingSelector:@selector(compare:)];//receiver只能是可变数组。排序时直接修改receiver,不会返回任何数据

       //  NSLog(@"%@", marray);

        [marray removeAllObjects];//清空容器

        NSLog(@"%@", marray);

        NSLog(@"Hello, World!");

    }

    return 0;

}

原文地址:https://www.cnblogs.com/about-zj-blog/p/5306215.html