iOS入门(十九)NSString NSArray NSDictionary

NSString NSArray NSDictionary
NSString   内容和长度不可改 
Format 格式串
string 字符串             NSString 不可变字符串类,对象创建以后,内容和长度都不可以更改。
字符串的十种基本操作

    //实例方法创建字符串

//    NSString *  str1 = [[NSString alloc]initWithFormat:@"HEllo !!world!!!"];

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

//    //便利构造器创建字符串

//    NSString * str2 = [NSString stringWithFormat:@"hello world"];

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

//    //获取字符串长度

//    NSLog(@"%lu", [str2 length]);

//    // 判断字符串是否已指定字符串开始或者结束

//    if ([str1 hasPrefix:@"HEllo"]) {

//        NSLog(@"没错,就是hello 开头");

//    }else {

//        NSLog(@"错了");

//    }

//    if ([str2 hasSuffix:@"hello"]) {

//        NSLog(@"没错!");

//    }

//    else{

//        NSLog(@"不是");

//    }

//    //搜索字符串范围

//    NSRange range = [str1 rangeOfString:@"HEllo"];

//    NSLog(@"%@" ,NSStringFromRange(range));

//    //字符串截取

//    NSString *sub1 = [str1 substringWithRange:range];

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

//    NSString * sub2 = [str2 substringFromIndex:3 ];

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

//    NSString *sub3 = [str1 substringToIndex:3];

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

//    str1 = [str1 substringToIndex:5];

    

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

    //字符串拼接

//    NSString * str1 = [NSString stringWithFormat:@"来一串"];

    NSString * str2 = [NSString stringWithFormat:@"%@%@",str1,@"羊肉串" ];

    NSLog(@"%@",str2);

//     str1 = [str1 stringByAppendingString:@"羊肉串"];

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

    //替换字符串

//    NSString * str = [NSString stringWithFormat:@"文艺文艺青年欢乐多~"];

    str = [str stringByReplacingOccurrencesOfString:@"文艺" withString:@"2b"];

//    NSRange range = {2,4};

//    str = [str stringByReplacingCharactersInRange:range withString:@"2b"];

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

    //字符串比较

//    NSString * str1 = @"138";

//    NSString * str2 = @"238";

//    if ( [str1 compare:str2] == NSOrderedAscending){

//        NSLog(@"升序");

//    }else if ([str1 compare:str2] == NSOrderedDescending){

//        NSLog(@"降序");

//    }else {

//        NSLog(@"same");

//    }

//    if ([str1 isEqualToString:str2]) {

//        NSLog(@"一样");

//    }else {

//        NSLog(@"不一样");}

    //字符串和数值类型转换

//    NSString * str = [NSString stringWithFormat:@"9526"];

//    int a = [str intValue];

//    a ++;

//    str = [NSString stringWithFormat:@"%d",a];

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

//    NSString * str1 = [NSString stringWithFormat:@"9526"];

//    NSString * str2 = [NSString stringWithFormat:@"1234"];

//    int b = [str2 intValue] + [str1 intValue];

//    NSString * result = [NSString stringWithFormat:@"%d" ,b];

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

    //大小写转换

//    NSString * str = [NSString stringWithFormat:@"hello! world~"];

//    //大写

//    str = [str uppercaseString];

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

//    //小写

//    str = [str lowercaseString];

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

//    //首字母大写

//    str = [str capitalizedString];

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



NSMutableString 可变字符串类 是NSString的子类  支持增删改查等动态的操作。
不可变字符串的修改方法以string开头,有返回值,本质就是得到一个新的字符串。可变字符串的修改方法没有返回值。修改原字符串,用操作开头。

    //字符串替换

 

    NSMutableString * str = [NSMutableString stringWithFormat:@"靡不有初,鲜克有终"];

    [str replaceCharactersInRange:NSMakeRange(5, 4) withString:@"善始善终"];

    NSLog(@"%@" , str );

//    [str setString:@"ABC"];

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

    [str deleteCharactersInRange:NSMakeRange(4, 5)];

    NSLog(@"%@" , str);

    [str insertString:@"好啊~" atIndex:3];

    NSLog(@"%@", str);

    [str appendString:@"啦啦~"];

    NSLog(@"%@" , str);


数组对象 
NSArray   不可变数组
容器类,管理一组对象类型的数据
数组中存储的元素必须是对象,类型任意。

    NSArray * arr = [[NSArray alloc] initWithObjects:@"付金诗", @"郑博" @"张静静" @"objectof张静静" @"裴紫夷", nil];

    NSLog(@"%lu" ,[arr count]);

    for (NSString * str  in arr) {

        NSLog(@"%@" , str);

    }

       NSLog(@"%lu"   , [arr  indexOfObject:@"张静静"]);

    //    NSString * str = [NSString stringWithFormat:@"The NSString class declares the programmatic interface for an object that manages immutable strings. An immutable string is a text string that is defined when it is created and subsequently cannot be changed. NSString is implemented to represent an array of Unicode characters, in other words, a text string."];

//    //将单词放在数组里

//    NSArray * arr = [str componentsSeparatedByString:@" "];

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

    

//    NSMutableArray * arr = [NSMutableArray  array ];

//    [arr addObject:@"language C"];

//    [arr addObject:@"C++"];

//    [arr addObject:@"Object - C"];

//    [arr addObject:@"C#"];

//    [arr addObject:@"JAVA "];

//    [arr addObject:@"html "];

//    [arr addObject:@"PHP "];

//    [arr exchangeObjectAtIndex:0 withObjectAtIndex:5];

//    [arr insertObject:@"C# " atIndex:3];

    [arr removeObject:@"PHP " inRange:NSMakeRange(4, 3)];

    [arr removeLastObject];

    [arr removeObject:@"JAVA "];

    [arr removeObjectAtIndex:3];

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

//    NSArray  * arrr = [NSArray arrayWithObjects:0,2,nil];


NSNumber   与基本数据类型的转换

//    NSMutableArray * arr = [NSMutableArray array];

//    NSNumber * num1 = [[NSNumber alloc] initWithInt:5];

//    [arr addObject:num1];

//    int value = [num1 intValue];

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

//    NSLog(@"%d" , value);

NSMutableArray   可变数组

 

    //截取字符串“20|http://www.baidu.com”中 “|” 前⾯面和后⾯面的字符串,并输出。

//    NSString * str = [NSString stringWithFormat:@"20|http : //www.baidu.com"];

//    NSString * str1 = [str substringToIndex:2];

//    NSString * str2 = [str substringFromIndex:3];

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

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

    // 将“⽂艺⻘青年”改成“213⻘青年”。

//    NSMutableString * str = [NSMutableString stringWithFormat:@"文艺青年"];

//    [str replaceCharactersInRange:NSMakeRange(0, 2) withString:@"213"];

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值