zoukankan      html  css  js  c++  java
  • OC 数组以及字符串拼接与分割

    //@""空的字符串对象-------分割
    NSString * ptr = @"I am a man";
    NSArray * array = [ptr componentsSeparatedByString:@" "];//将字符串整体作为分割条件 返回值为NSArray不可变数组
    NSMutableArray * array1 = [NSMutableArray arrayWithArray:array];//若修改,则将NSArray转化为NSMutableArray
    NSArray * array2 = [ptr componentsSeparatedByCharactersInSet:[NSCharacterSet                                           
    characterSetWithCharactersInString:@", "]];//以字符串中的字符作为分割条件进行分割
    《字符集合》
    NSCharacterSet 这是个字符集合类
    [NSCharacterSet characterSetWithCharactersInString:@", "]//把字符串转化为字符集合
    -------拼接
    NSString * str = [array componentsJoinedByString:@" "];
    若分割条件出现在开头或者结尾,则会出现空串@"",如果不需要,则需要转化成NSMutableString对空串进行处理
    func1:  [array1 removeObject:@""];     //找到空串直接删除
    func2:  for(id obj in array1){
     if([obj length] == 0)           //空串的长度为0
     if([obj isEqualToString:@""])   //与空串进行比较(字符串是不能进行==比较的,要使用函数)       
    }
     
    可变数组:
    NSMutableArray * array = [[NSMutableArray alloc]initWithObjects:@"one",@"two",@"three",@"four", nil];
    [array addObject:@"five"];//在数组尾部插入元素
    [array insertObject:@"six" atIndex:5];//在数组指定下标位置插入元素(不能越界,最大值为length)
     
    [array removeObject:@"six"];//删除指定元素(数组中出现的所有位置都将被删除)
    [array removeObject:@"two" inRange:NSMakeRange(0, 3)];//从指定下标位置开始的指定长度内删除指定元素
    [array removeLastObject];//删除最后一个元素
    [array removeAllObjects];//删除所有元素
         
    [array replaceObjectAtIndex:3 withObject:@"ios"];//将指定下标位置元素替换为指定的元素
    [array exchangeObjectAtIndex:0 withObjectAtIndex:3];//将指定下标的两个元素进行交换
     
    不可变数组:
    NSArray * array = [[NSArray alloc]initWithObjects:@"one",@"two",@"three",@"one", nil];
     
    NSUInteger index = [array indexOfObject:@"one123"];//返回第一个找到的数组成员对应的下标 找不到返回NSNotFound
     
    index = [array indexOfObject:@"one" inRange:NSMakeRange(1, 3)];//在指定范围内查找
        if (index != NSNotFound) {
             NSLog(@"%ld",index);
        }
    ----抽取 组成新的数组
    NSArray * array1 = [array objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)]];
    《数字集合》
    NSIndexSet这是个数字集合类
    [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)]产生一个数字集合
     
    原文地址:http://my.oschina.net/outatu/blog/124967
  • 相关阅读:
    【转载】opencvVS2019配置方法
    windows.h头文件中改变光标位置的函数——SetConsoleCursorPosition
    五行代码解决猴子选大王问题
    AtCoder Beginner Contest 192
    ACM做题注意事项
    数据库部分重点
    数据库7-11章期末复习
    数据库4-6章期末复习
    数据库1-3章期末复习
    ICPC Central Russia Regional Contest (CRRC 19)
  • 原文地址:https://www.cnblogs.com/littlewrong/p/4895074.html
Copyright © 2011-2022 走看看