IOS学习笔记十六(NSString和NSMutableString)

举报
chenyu 发表于 2021/07/26 23:40:07 2021/07/26
【摘要】 1、NSString和NSMutableString NSString是不变字符串类,有点像java里面的String,NSMutableString是可变字符串类,有点类似java里面的StringBuffer   2、测试demo int main(int argc, char * argv[]) { @autoreleasepool { unicha...

1、NSString和NSMutableString

NSString是不变字符串类,有点像java里面的String,NSMutableString是可变字符串类,有点类似java里面的StringBuffer

 


2、测试demo


  
  1. int main(int argc, char * argv[]) {
  2. @autoreleasepool {
  3. unichar data[6] = {97, 98, 100, 101, 102, 103};
  4. NSString *str = [[NSString alloc] initWithCharacters:data length:6];
  5. NSLog(@"str is %@", str);
  6. char *cstr = "chenyu";
  7. NSString *str2 = [NSString stringWithUTF8String:cstr];
  8. NSLog(@"str2 is %@", str2);
  9. NSString *str3 = @"chenyu";
  10. NSString *name = @"陈喻";
  11. str3 = [str3 stringByAppendingString:@"chenyu"];
  12. NSLog(@"str3 is %@", str3);
  13. const char *cstr1 = [str3 UTF8String];
  14. NSLog(@"cstr1 is %s", cstr1);
  15. str3 = [str3 stringByAppendingFormat:@"hello %@ hello", name];
  16. NSLog(@"str3 is %@", str3);
  17. NSLog(@"str3 length is %lu", [str3 length]);
  18. NSString *s1 = [str3 substringToIndex:10];
  19. NSLog(@"s1 is %@", s1);
  20. NSString *s2 = [str3 substringFromIndex:5];
  21. NSLog(@"s2 is %@", s2);
  22. NSString *s3 = [str3 substringWithRange:NSMakeRange(5,10)];
  23. NSLog(@"s3 is %@", s3);
  24. NSRange pos = [str3 rangeOfString:@"陈喻"];
  25. NSLog(@"陈喻在str3中开始的位置:%ld,长度为%ld", pos.location, pos.length);
  26. str3 = [str3 uppercaseString];
  27. NSLog(@"str3 is %@", str3);
  28. NSMutableString *tstr = [NSMutableString stringWithString:@"hello"];
  29. [tstr appendString:@"chenyu"];
  30. NSLog(@"tstr is %@", tstr);
  31. [tstr appendFormat:@"hello word %@", @"chengongyu"];
  32. NSLog(@"tstr is %@", tstr);
  33. [tstr insertString:@"hello" atIndex:6];
  34. NSLog(@"tstr is %@", tstr);
  35. [tstr deleteCharactersInRange:NSMakeRange(6, 9)];
  36. NSLog(@"tstr is %@", tstr);
  37. [tstr replaceCharactersInRange:NSMakeRange(3, 6) withString:@"objectobject"];
  38. NSLog(@"tstr is %@", tstr);
  39. }
  40. }

 

 

 

 

 

 

3、运行结果


  
  1. str is abdefg
  2. str2 is chenyu
  3. str3 is chenyuchenyu
  4. cstr1 is chenyuchenyu
  5. str3 is chenyuchenyuhello 陈喻 hello
  6. str3 length is 26
  7. s1 is chenyuchen
  8. s2 is uchenyuhello 陈喻 hello
  9. s3 is uchenyuhel
  10. 陈喻在str3中开始的位置:18,长度为2
  11. str3 is CHENYUCHENYUHELLO 陈喻 HELLO
  12. tstr is hellochenyu
  13. tstr is hellochenyuhello word chengongyu
  14. tstr is hellochellohenyuhello word chengongyu
  15. tstr is hellocuhello word chengongyu
  16. tstr is helobjectobjectllo word chengongyu

 

 

 

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/81008266

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。