博客> 7.NSMutableString
7.NSMutableString
2018-10-23 09:46 评论:0 阅读:461 Longer_Tao
oc NSMutableString
#pragma mark - Foundation框架之  NSMutableString
/********************************** NSMutableString *********************************
NSMutableString 类 继承自NSString类,故具有NSString所有的方法(但是都没有返回值)
注意事项:
 @”lnj”这种方式创建的字符串始终是NSString,不是NSMutalbeString.所以下面的代码创建的还是NSString,
 此时使用可变字符串的函数,无法操作字符串。
 NSMutalbeString *s1 = @”lnj”; // 会报错 [strM insertString:@"my name is " atIndex:0];
*/
#pragma mark 常用方法
// 1> 拼接aString到最后面
 - (void)appendString:(NSString *)aString;

// 2>  拼接一段格式化字符串到最后面
 - (void)appendFormat:(NSString *)format, ...;
// 3> 删除range范围内的字符串 
 - (void)deleteCharactersInRange:(NSRange)range;

  NSMutableString *strM = [NSMutableString stringWithString:@"http://www.520it.com"];
  // 一般情况下利用rangeOfString和deleteCharactersInRange配合删除指定内容
  NSRange range = [strM rangeOfString:@"http://"];
  [strM deleteCharactersInRange:range];
  NSLog(@"strM = %@", strM);
// 4> 在loc这个位置中插入aString 
 - (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;

  NSMutableString *strM = [NSMutableString stringWithString:@"www.520it.com"];
  [strM insertString:@"http://" atIndex:0];
  NSLog(@"strM = %@", strM);

// 5> 使用aString替换range范围内的字符串 
 - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;

  NSMutableString *strM = [NSMutableString stringWithString:@"http://www.520it.com/lnj.png"];
  NSRange range = [strM rangeOfString:@"lnj"];
  // [strM replaceOccurrencesOfString:@"lnj" withString:@"jjj" options:0 range:range];
  [strM replaceCharactersInRange:range withString:@"loong"]; // 待验证
  NSLog(@"strM = %@", strM);
收藏
0
sina weixin mail 回到顶部