zoukankan      html  css  js  c++  java
  • 进击的Objective-C -------------------NSSting,NSSMutableString和NSArray,NSMutableArray

    NSString 
    1.赋值
    NSString *s1 = @“123”;
    2.初始化方法
    NSString *s2 = [[NSString alloc]init];
    3. 便利构造器
    NSString *s3 = [NSString string];
    4.格式化初始化字符串
    NSString *s4 = [[NSString alloc]initWithFormat:@“boomshakalaka”];
    NSString *s5 = [NSString stringWithFormat:@“lalalala”];
    NSString *s6 = [NSString stringWithFormat:@“%@shakalaka”,@“boom”];
    NSString *ss = [NSString stringWithFormat:@“%dlalala”,8];
    5.通过路径获取字符串
    NSString * s7 = [NSString stringWithContentsOfFile:@“路径”encoding:NSUTF8StringEncoding error:nil];
    6.字符串长度g        
    NSlog(@“%lu“,[s6 length];
    7.判断是否有前后缀
    NSString *s8 = @“abc123”;
    BOOL b1 = [s8 hasPrefix:@“abc”];
    BOOL b2 = [s8 hasSuffix:@“3”];
    NSLog(@“%d”,b1);
    NSLog(@“%d”,b2);
    8.大小写转换
    小->大
    NSString * s9 = [s8 uppercaseString];
    NSLog(@“%@“,s9);
    大->小
    NSString * s10 = [s9 lowercaseString];
    NSLog(@“%@“,s10); 
    9.比较两个字符串大小
    NSComparisonResult r1 = [s8 compare:@“b”];
    NSLog(@“%ld”,r1);
    10.比较字符串是否相等
    BOOL b3 = [s8 isEqualToString:s9];
    NSLog(@“%d”,b3);
    11.获取某个位置的字符
    NSLog(@“%c”,[s8 characterAtIndex:1]);
    NSString * s11 = [s8 substringFromIndex:1];
    NSLog(@“%@“,s11);
    NSString * s12 = [s8 substringToIndex:1];
    NSLog(@“%@“,s12);
    NSString * s13 = [s8 substringWithRange:NSMakRange(3,3)];
    NSLog(@“%@“,s13);
    NSMutableString
     
    1.赋值
    NSMutableString *ms1 = [NSMutableString stringWithFormat:@“456”];
    NSLog(@“%@“,ms1);
    2.改值或设置值
    [ms1 setString:@“789”];
    NSLog(@“%@“,ms1);
    3.拼接
    [ms1 appendString:@“123”];
    NSLog(@“%@“,ms1);
    [ms1 appendFormat:@“123%d”,5];
    NSLog(@“%@“,ms1);
    4.替换
    [ms1 replaceCharatersInRange:NSMakeRange(3,3) withString:@“456”];
    NSLog(@“%@“,ms1);
    5.删除
    [ms1 deleteCharatersInRange:NSMakeRange(9,1)];
    NSLog(@“%@“,ms1);
    6.插入
    [ms1 insertString:@“0” atIndex:0];
    NSLog(@“%@“,ms1);
    7.首字母大写
    NSMutableString *aa = [NSMutableString stringWithFormat:@"i love you"];
    NSString *ss = [aa capitalizedString];
    NSLog(@"%@",ss);
    NSArray
    1.初始化方法
    NSArray *arr1 = [[NSArray alloc]initWithObjects:@“1”,@“2”,@“3”,nil];
    NSLog(@“%@”,arr1);
    2.便利构造器
    NSArray *arr2 = [NSArray arrayWithObjects:@“4”,@“5”,@“6”,nil];
    NSLog(@“%@“,arr2);
    3.利用数组初始化数组
    NSArray *arr3 = [NSArray arrayWithArray:arr2 ];
    NSLog(@“%@”,arr3);
    4. 字面量初始化数组
    NSArray *arr4 = @[@“7”,@“8”,@“9”];
    NSLog(@“%@”,arr4);
    5.计算数组元素个数
    NSInteger ui1 = [arr4 count];
    NSLog(@“%lu”,ui1);
    6.取出数组元素下标
    NSINteger ui2 = [arr4 indexOfObject:@“9”];
    NSLog(@“%lu”,ui2);
    7.取出下标对应的元素
    NSLog(@“%@“,[arr4 objectAtIndex:1]);
    NSMutableArray
    1.初始化数组
    NSMutableArray *mArr1 = [NSMutableArray arrayWithObjects:@"1",@"2",@"3", nil];
    2.添加
    [mArr1 addObject:@"4"];
    [mArr1 addObject:@"5"];
    NSLog(@"%@",mArr1);
    3.删除
    [mArr1 removeLastObject];-[mArr1 removeAllObjects];//删除全部
    NSLog(@"%@",mArr1);
    [mArr1 removeObject:@"4"];
    NSLog(@"%@",mArr1);
    4.删除下标所在元素
    [mArr1 removeObjectAtIndex:0];
    NSLog(@"%@",mArr1);
    5. 插入
    [mArr1 insertObject:@"1"atIndex:0];
    NSLog(@"%@",mArr1);
    6.替换
    [mArr1 replaceObjectAtIndex:0withObject:@"3"];
    [mArr1 replaceObjectAtIndex:1 withObject:@"3"];
    NSLog(@"%@",mArr1);
    NSNumber  
    1.将基本数据类型转换成对象类型
    int i = 1;
    long l = 20000;
    NSNumber *number1 = [[NSNumber alloc]initWithInt:i];
    2.NSNumber 类型也可以转化成int类型
    int a1 = [number1 intValue];
    NSLog(@"%d",a1);
    NSNumber *number2 = [[NSNumber alloc]initWithLong:l];
    NSLog(@"%@",number1);
    NSLog(@"%@",number2);
    NSString *string = @"123";
    3.将字符串转化成 整型
    int a2 = [string intValue];
    NSLog(@"%d",a2);
    4.将NSNumber转化成 字符串
    [number2 stringValue];
    NSLog(@"%@",number2);
     
  • 相关阅读:
    PAT (Basic Level) Practice (中文)1002 写出这个数 (20 分)
    PAT (Advanced Level) Practice 1001 A+B Format (20 分)
    BP神经网络(原理及MATLAB实现)
    问题 1676: 算法2-8~2-11:链表的基本操作
    问题 1744: 畅通工程 (并查集)
    链表的基本操作(创建链表,查询元素,删除元素,插入元素等)
    问题 1690: 算法4-7:KMP算法中的模式串移动数组
    问题 1923: [蓝桥杯][算法提高VIP]学霸的迷宫 (BFS)
    Hdu1372 Knight Moves (BFS)
    Problem 2285 迷宫寻宝 (BFS)
  • 原文地址:https://www.cnblogs.com/sharkHZ/p/4984027.html
Copyright © 2011-2022 走看看