1.NSMutableString常用方法

  • - (void)appendString:(NSString *)aString;

    • 拼接aString到最后面
   NSMutableString *strM = [NSMutableString string];
NSLog(@"strM = %@", strM);
[strM appendString:@"lnj"];
NSLog(@"strM = %@", strM);
  • - (void)appendFormat:(NSString *)format, ...;

    • 拼接一段格式化字符串到最后面
NSMutableString *strM = [NSMutableString string];
[strM appendFormat:@"/age is %i", 10];
  • - (void)deleteCharactersInRange:(NSRange)range;

    • 删除range范围内的字符串
 NSMutableString *strM = [NSMutableString stringWithString:@"http://www.520it.com"];
// 一般情况下利用rangeOfString和deleteCharactersInRange配合删除指定内容
NSRange range = [strM rangeOfString:@"http://"];
[strM deleteCharactersInRange:range];
NSLog(@"strM = %@", strM);
  • - (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;

    • 在loc这个位置中插入aString
 NSMutableString *strM = [NSMutableString stringWithString:@"www.520it.com"];
[strM insertString:@"http://" atIndex:0];
NSLog(@"strM = %@", strM);
  • - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;

    • 使用aString替换range范围内的字符串
    NSMutableString *strM = [NSMutableString stringWithString:@"http://www.520it.com/lnj.png"];
NSRange range = [strM rangeOfString:@"lnj"];
[strM replaceOccurrencesOfString:@"lnj" withString:@"jjj" options:0 range:range];
NSLog(@"strM = %@", strM);

2.字符串使用注意事项

  • @”lnj”这种方式创建的字符串始终是NSString,不是NSMutalbeString.所以下面的代码创建的还是NSString,此时使用可变字符串的函数,无法操作字符串。
NSMutalbeString *s1 = @”lnj”;
// 会报错
[strM insertString:@"my name is " atIndex:0];

NSMutableString常用方法的更多相关文章

  1. NSString和NSMutableString常用方法+NSArray常用代码 (转)

    常见的NSString和NSMutableString方法: NSString方法: [plain] view plaincopy   +(id) stringWithContentsOfFile:p ...

  2. 字符串NSString与NSMutableString常用方法

    NSString 1.初始化 NSString *str1 = @"a OC Program"; 2.初始化 NSString *str2 = [[NSString alloc] ...

  3. Foundation框架-NSString和NSMutableString

    可变与不可变的字符串 --1-- Foundation框架介绍 1.1 框架介绍 --2-- NSString 2.1 NSString介绍及使用 2.2 NSString创建方式  2.3 从文件中 ...

  4. Foundation框架

    1.框架是由许多类.方法.函数.文档按照一定的逻辑组织起来的集合,以便使研发程序变的更容易 清除缓存,删除这个文件夹下的所有文件/Users/fanyafang/Library/Developer/X ...

  5. 学习ios【2】Objective-C 数字和字符串

    一 数字 1.使用Foundation.h可以直接导入所有的头文件. 在XCode中,想查看某个方法帮助,可以将光标放在方法上,按住option键同时单击即可. 官方文档:https://develo ...

  6. OC基础(22)

    NSMutableString基本概念 NSMutableString常用方法 NSMutableString练习 *:first-child { margin-top: 0 !important; ...

  7. OC基础笔记目录

    OC基础(1) Objective-C简介 OC和C对比 第一个OC程序 面向对象思想 OC基础(2) 类与对象 类的设计 第一个OC类 对象方法的声明和实现 类方法的声明和实现 OC基础(3) 对象 ...

  8. oc69--NSMutableString

    // main.m // NSMutableString基本概念,NSString是不可变字符串,NSMutableString是可变字符串.NSMutableString继承NSString,所以N ...

  9. OC-基础数据类型

    七 字符串与基本数据类型转换 获取字符串的每个字符/字符串和其他数据类型转换 八 NSMutableString 基本概念/常用方法 九 NSArray NSArray基本概念/创建方式/注意事项/常 ...

随机推荐

  1. 编写Java程序,在维护英雄数据的业务过程中复用数据库操作封装类

    返回本章节 返回作业目录 需求说明: 继续完善英雄信息系统,将HeroAccess类中数据库连接相关代码独立出来,并将其封闭成数据库操作工具类DbConnection,便于管理和代码复用. 在Hero ...

  2. win10系统微软账号登陆错误报错误码0x80190001

    https://blog.csdn.net/u012878537/article/details/91353248 使用onenote同步的时候报出这样的错误. 知道问题出在了哪里,就容易解决了不是. ...

  3. [ vue ] xxxProject项目杂记

    2020.4.9 加入eCharts 2020.4.8 完成article的显示,其间碰到全局路由守卫写的有错误,导致跳转报错.已修复. 加入keep-alive功能,缓存视图数据 疑问:如果在全局组 ...

  4. CentOS 7 使用unzip解压zip文件提示未找到命令的解决方法

    故障现象: 解决方法: 如果你使用unzip命令解压.zip文件,提示未找到命令,可能是你没有安装unzip软件,下面是安装方法 [root@localhost www]# yum install - ...

  5. 第10组 Beta冲刺 (4/5)

    1.1基本情况 ·队名:今晚不睡觉 ·组长博客: https://www.cnblogs.com/cpandbb/p/14018650.html ·作业博客:https://edu.cnblogs.c ...

  6. 实验 6 :OpenDaylight 实验——OpenDaylight 及 Postman实现流表下发

    实验 6 :OpenDaylight 实验--OpenDaylight 及 Postman实现流表下发 一.实验目的 熟悉 Postman 的使用:熟悉如何使用 OpenDaylight 通过 Pos ...

  7. android入门--环境搭建

    运行环境 windows 7 下载地址 环境下载 最近开接触Android(安卓)嵌入式开发,首要问题是搭建Andoid开发环境,由于本人用的是windows7的笔记本,也就只能到Windows中搭建 ...

  8. 移动端H5选择本地图片

    移动端H5选择本地图片 html://input<input type="file" accept="image/*" capture="cam ...

  9. 微服务架构 | 3.3 Apache Zookeeper 注册中心

    @ 目录 前言 1. Zookeeper 基础知识 1.1 Zookeeper 是什么 1.2 Zookeeper 的数据结构 1.3 Watcher 机制 1.4 常见应用场景分析 1.5 Zook ...

  10. json 转换C# class(用于对接api

    //说明//使用场景:对接api,返回json结果,直接转换C# class//如何使用:复制下面js代码在浏览器控制台执行 ` "order_item_id": "28 ...