A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. //1.  比较字符串的内容是否相同
  2. NSString *string6=[[NSString alloc]initWithFormat:@"%d",number];

  3. NSString *string7=[NSString stringWithFormat:@"%d",number];

  4. NSLog(@"%@,%@,%@,%@,%@,%@,%@",string1,string2,string3,string4,string5,string6,string7);

  5. //2.  比较两个字符串是否相等
  6. if([string6 isEqualToString:string7]){
  7.     NSLog(@"相同内容");
  8. }else{
  9.     NSLog(@"不同内容");
  10. }


  11. //3.  比较两个字符串是否为同一个对象
  12. NSString *string8=[NSString stringWithString:@"123"];

  13. NSString *string9=[NSString stringWithString:@"123"];

  14. if([string8 isEqualToString:string9]){
  15.     NSLog(@"相同内容");
  16. }else{
  17.     NSLog(@"不同内容");
  18. }
  19. if(string8==string9){
  20.     NSLog(@"同一个对象");
  21. }else{
  22.     NSLog(@"不同对象");
  23. }


  24. //4.  比较字符串的大小
  25. NSString *string10=[[NSString alloc]initWithString:@"a"];

  26. NSString *string11=[[NSString alloc]initWithString:@"b"];

  27. NSComparisonResult result=[string10 caseInsensitiveCompare:string11];

  28. NSLog(@"%ld",result); //-1表示升序


  29. //5.  创建字符串还有如下一些常用的方法:
  30. - (id)init;
  31. - (id)initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer; /* "NoCopy" is a hint */
  32. - (id)initWithCharacters:(const unichar *)characters length:(NSUInteger)length;
  33. - (id)initWithUTF8String:(const char *)nullTerminatedCString;
  34. - (id)initWithString:(NSString *)aString;
  35. - (id)initWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
  36. - (id)initWithFormat:(NSString *)format arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);
  37. - (id)initWithFormat:(NSString *)format locale:(id)locale, ... NS_FORMAT_FUNCTION(1,3);
  38. - (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);
  39. - (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;
  40. - (id)initWithBytes:(const void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding;
  41. - (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)freeBuffer; /* "NoCopy" is a hint */

  42. + (id)string;
  43. + (id)stringWithString:(NSString *)string;
  44. + (id)stringWithCharacters:(const unichar *)characters length:(NSUInteger)length;
  45. + (id)stringWithUTF8String:(const char *)nullTerminatedCString;
  46. + (id)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
  47. + (id)localizedStringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

  48. - (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;
  49. + (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;

  50. /* These use the specified encoding.  If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors).
  51. */
  52. - (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;
  53. - (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
  54. + (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;
  55. + (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;

  56. /* These try to determine the encoding, and return the encoding which was used.  Note that these methods might get "smarter" in subsequent releases of the system, and use additional techniques for recognizing encodings. If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors).
  57. */
  58. - (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;
  59. - (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;
  60. + (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;
  61. + (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;

  62. //6.  求字符串的长度
  63. NSString  *string12=[[NSString alloc]initWithString:@"abcd"];

  64. NSLog(@"string2的长度为:%ld",[string12 length]);

  65. //7.  字符串的转换
  66. NSString *string13=[[NSString alloc]initWithString:@"HelloWorld"];

  67. NSLog(@"upper %@",[string13 uppercaseString]);

  68. NSLog(@"lower %@",[string13 lowercaseString]);

  69. NSLog(@"capitalized %@",[string13 capitalizedString]);

  70. //8.  字符串转换成基本数据类型
  71. NSString *string14=[[NSString alloc]initWithString:@"3.14"];

  72. NSLog(@"%.2f\n",[string14 floatValue]);

  73. //9.  字符串转换成数组
  74. NSString *string15=[[NSString alloc]initWithString:@"abc bcd xyz"];

  75. NSArray *arra=[string15 componentsSeparatedByString:@""];

  76. NSLog(@"%@\n",array);

  77. //10.  字符串的截取
  78. NSString *string16=@"abcdefg";

  79. NSLog(@"%@",[string16 substringToIndex:2]);

  80. NSLog(@"%@",[string16 substringFromIndex:2]);

  81. NSRange range; range.location=2; range.length=3;

  82. NSLog(@"%@",[string16 substringWithRange:range]);

  83. //11.  字符串的拼接
  84. NSString *string17=@"abc";

  85. NSString *string18=@"xyz";

  86. NSString *appString=[[NSString alloc]initWithFormat:@"这是拼接的字符串:%@ and %@",string17,string18];

  87. NSString *appString2=[string17 stringByAppendingString:@"123"];

  88. NSString *appstring3=[string17 stringByAppendingFormat:@"%@",string18];

  89. NSLog(@"%@,%@,%@",appString,appString2,appstring3);

  90. //12.  字符串的查找
  91. NSString *link=@"abcdffe-===fefjfwfw";

  92. NSRange range1=[link rangeOfString:@"abcd"];

  93. NSLog(@"%@",NSStringFromRange(range1));

  94. if(range1.location != NSNotFound){
  95.     NSLog(@"found!");
  96. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
星河鹭起 + 1

查看全部评分

1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马