常用类---字符串

来源:互联网 发布:淘宝评价可以改差评吗 编辑:程序博客网 时间:2024/05/21 10:58

字符串:NSString、NSMutableString
 1. 创建:
  NSString *s = @"123";
  NSString *s = [[NSString alloc] initWithFormat:@"123%@", @"123"];
 2. 格式转化:
  double d = [s floatValue];
  int i = [s intVlue];
 3. 长度:
  int lenght = [s lenght];
 4. 比较:
  BOOL b = [s1 isEqualToString:s2];
         区分大小写的比较:
  BOOL b = ( [ s1 caseInsensitiveCompare:s2 ] == NSOrderSame);
 5. 大小写转换:
  s = [ s1 uppercaseString ];
  s = [ s1 lowercase String ];
 6. 删除空白:
  s = [ s1 stringByTrimmingCharactersInset: [ NSCharacterSet  whitespaceCharacterSet]];
 7. 截取字符串:
  s = [s substringToIndex: index]; // s = 0--(index-1)
  s = [s substringWithRange:[NSMakeRange(start, end)]]; 
  s = [s substringFromIndex: index ] ; // s = index -- (length -1)
 8. 替换:
  s = [ s stringByReplacingOccurencesOfString:subS1  withString:subS2 ];
 9. 组合:
  s = [s1 stringAppendingString:s2];
 10. 是否包含:
  BOOL b = ( [s rangeOfString:subS].location != NSNotFound);
 11. 取文件内容:
  s = [NSString stringWithContentsOfFile: filenameString];
 12. 取文件扩展名:
  s = [fileNameString pathExtension];
 13. 读取URL内容:
  s = [NSString stringWithContentsOfURL:url];