OC_字符串NSString

 //输入一个字符串 

NSString *str=@"sunxiang孙祥";

//1.求字符串长度

   NSLog(@"%ld",str.length);

打印结果:2015-07-20 17:28:32.700 0720练习[2083:303] 10

//2.characterAtIndex:通过指定下标获取字符串内容,%c-打印字母,%C-打印汉字

    NSLog(@"%c",[strcharacterAtIndex:5]);

    NSLog(@"%C",[strcharacterAtIndex:9]);

打印结果:2015-07-20 17:28:32.702 0720练习[2083:303] a

        2015-07-20 17:28:32.702 0720练习[2083:303]


//3.1substring:截取字符串

//substringFromIndex:从输入的下标开始截取,包括下标,到最后

//substringToIndex:从开始到输入的下标,不包括下标

    NSString *str=@"hello,my name is guodanni";

    NSLog(@"%@",[strsubstringFromIndex:6]);

    NSLog(@"%@",[strsubstringToIndex:6]);

打印结果:2015-07-20 17:42:12.859 0720练习[2126:303] my name is             guodanni

       2015-07-20 17:42:12.861 0720练习[2126:303] hello,


//3.2字符串的截取

//3.2.1 NSRange是一个结构体,提供了两个成员变量,一个是location起始的位置,还有一个是长度

    NSString *str=@"hello,my name is guodanni";

   NSRange range={6,2};

    NSLog(@"%@",[strsubstringWithRange:range]);

//3.2.2把上面两条变为一条语句的格式

                     NSLog ( @"%@" ,[str substringWithRange : NSMakeRange ( 6 ,   2 )]);

 打印结果:2015-07-20 17:52:40.382 0720练习[2159:303] my

//4.判断两个字符串的内容是否相同

   NSString *str1=@"lucky";

   NSString *str2=@"lucky";

   NSLog(@"%d",[str1isEqualToString:str2]);

    //注意:if(str1==str2)是比较两个字符串的地址

打印结果:2015-07-20 17:58:42.215 0720练习[2198:303] 1

//5.是否有前缀或后缀

    NSString *str=@"victorycometome";

   NSString *prefix=@"victory";

   NSString *suffix=@"me";

   NSLog(@"%d",[strhasPrefix:prefix]);

   NSLog(@"%d",[strhasSuffix:suffix]);


2015-07-20 18:06:21.708 0720练习[2246:303] 1

2015-07-20 18:06:21.710 0720练习[2246:303] 1

//6.把字符串转换成基本数据类型integerValue

   NSString *str=@"12345";

   NSInteger strNew=[strintegerValue];

   NSLog(@"%ld",strNew);

2015-07-20 18:11:36.529 0720练习[2267:303] 12345

//7.stringWithFormat:方法可以把任意类型转换成字符串类型,并且可以进行拼接

   CGFloat g=3.14;

    NSInteger a=100;

   NSString *str=[NSStringstringWithFormat:@"%g %ld %@",g,a,@"大家好"];

   NSLog(@"%@",str);

2015-07-20 18:16:46.076 0720练习[2295:303] 3.14 100大家好

//8.uppercaseString:所有字符都变大写

 //  lowercaseString:所有字符都变小写

  //capatializadString:首字母大写,若字符串里有空格,空格后的第一个字母大写

    NSString *str=@"IamWinner";

    NSLog(@"%@",str.uppercaseString);

    NSLog(@"%@",str.lowercaseString);

    NSString *str1=@"i am winner";

    NSLog(@"%@",str1.capitalizedString);

2015-07-20 18:35:59.988 0720练习[2331:303] IAMWINNER

2015-07-20 18:35:59.989 0720练习[2331:303] iamwinner

2015-07-20 18:35:59.989 0720练习[2331:303] I Am Winner

//9.compare:对应有三个结果,一个是ASC升序结果是-1,一个是相同same结果是0,降序DESC结果是1

   NSString  *str=@"hello";

   NSString *str1=@"hello";

   NSLog(@"%ld",[strcompare:str1]);

   NSString  *str2=@"hello1";

   NSString  *str3=@"hello2";

   NSLog(@"%ld",[str2compare:str3]);

   NSString  *str4=@"hello2";

   NSString  *str5=@"hello1";

   NSLog(@"%ld",[str4compare:str5]);

2015-07-20 18:42:33.394 0720练习[2385:303] 0

2015-07-20 18:42:33.396 0720练习[2385:303] -1

2015-07-20 18:42:33.396 0720练习[2385:303] 1

//10.stringByAppendingString:字符串的拼接

   NSString *str=@"hello";

    NSString *str1=@" my family member";

    NSLog(@"%@",[strstringByAppendingString:str1]);


2015-07-20 18:46:48.796 0720练习[2413:303] hello my family member

//11.stringByReplaceingOccurrencesOfString:字符串的替换

    //第一个参数:字符串原有的部分

    //第二个参数:要替换的内容

    NSString *str=@"hello lanou";

    NSLog(@"%@",[str           stringByReplacingOccurrencesOfString:@"lanou"   withString:@"family"]);

2015-07-20 18:51:35.201 0720练习[2421:303] hello family


//12.NSMutableString:可变字符串,他是NSString的子类(NSString:不可变字符串)

// NSMutableString *str=[[NSMutableString alloc] init];

//使用字面量的方式创建的是不可变字符串,只能拿不可变的指针来接收

// NSMutableString *str=@"充实的一天";

// NSMutableString *str=[[NSMutableString alloc] initWithString:@"今天"];

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


//13.insertString:插入字符串,第一个输入的是要插入的字符串,第二个是插入的位置(下标)

    NSMutableString *str=[NSMutableStringstringWithString:@"my family"];

    [str insertString:@"happy "atIndex:3];

   NSLog(@"%@",str);

2015-07-20 19:05:47.109 0720练习[2463:303] my happy family

//14.replaceCharactersInRange:字符串的替换

    NSMutableString *str=[NSMutableStringstringWithString:@"hello,my name is danny"];

    [str replaceCharactersInRange:NSMakeRange(0,5)withString:@"Hi"];

   NSLog(@"%@",str);


2015-07-20 19:12:06.411 0720练习[2487:303] Hi,my name is danny

//15.不可变字符串的拼接,并且原串不变

   NSString *str=@"hello";

    NSString *strNew=[strstringByAppendingString:@" how are you"];

   NSLog(@"%@",str);

   NSLog(@"%@",strNew);

2015-07-20 19:18:08.101 0720练习[2526:303] hello

2015-07-20 19:18:08.102 0720练习[2526:303] hello how are you

// 可变字符串在方法上一般都是没有返回值的 , 都是对自身内容进行修改 , 在访问的时候字符串已经发生了变化 , 但是不可变字符窜会产生一个新的字符串 , 一般会返回 NSString *.

NSMutableString *str=[[NSMutableStringalloc]initWithString:@"hello"];

    NSLog(@"%@",[strstringByAppendingString:@" world"]);

2015-07-20 19:26:48.073 0720练习[2549:303] hello world









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值