NSString NSMutableString
// NSString
//代开API文档
//Xcode -> help - Documentation and API Reference
//快速打开API
//alt(option) + 鼠标左键
//快速进入头文件
//command + 鼠标左键
// NSString是oc中的不可变字符串类,被创建后,不能修改
//常用方法
//1.创建字符串
NSString *str1 = @"iPhone";
NSLog(@"%@", str1);
NSString *str2 = [[NSString alloc] init];
NSLog(@"%@", str2);
NSString *str3 = [[NSString alloc] initWithString:@"iPhone"];
NSLog(@"%@", str3);
// NSString *str4 = [[NSString alloc] initWithString:nil];
// NSLog(@"%@", str4); error
NSString *str5 = [[NSString alloc] initWithFormat:@"%@5s", str1];
NSLog(@"%@", str5);
// NSString *str6 = [NSString stringWithString:@"iPhone"];
// NSLog(@"%@", str6);
NSString *str7 = [NSString stringWithFormat:@"123"];
NSLog(@"%@", str7);
//2.获取字符串长度
NSUInteger length = [str7 length];
NSLog(@"%lu", length);
//3.判断字符串是否以指定字符串开始或结束
NSString *str8 = @"123ABC";
BOOL result = [str8 hasPrefix:@"223"];
NSLog(@"%d", result);
BOOL result1 = [str8 hasSuffix:@"C"];
NSLog(@"%d", result1);
//4.搜索字符串的范围
NSRange range = [str8 rangeOfString:@"3A"];
NSLog(@"%lu, %lu", range.length, range.location);
//5.字符串截取
NSRange rs = {3, 3};
NSString *str9 = [str8 substringWithRange:rs];
NSLog(@"%@", str9);
//6.拼接字符串
NSString *str10 = [str8 stringByAppendingString:@"123"];
NSLog(@"%@", str8);
NSLog(@"%@", str10);
//7.替换字符串
NSString *str11 = [str8 stringByReplacingOccurrencesOfString:@"3A" withString:@"A"];
NSLog(@"%@", str11);
//8.字符串比较"123" "123" "321"
NSInteger flag = [@"123" compare:@"123"];
NSLog(@"%ld", flag);
//9.字符串和数值类型的转换
NSString *str12 = @"123";
int a = [str12 intValue];
NSLog(@"%d", a);
double b = [str12 doubleValue];
NSLog(@"%f", b);
float c = [str12 floatValue];
NSLog(@"%f", c);
BOOL e = [str12 boolValue];
NSLog(@"%d", e);
//10.大小写转换操作
NSString *aaa = @"aBcE";
NSLog(@"%@", [aaa capitalizedString]);
NSLog(@"%@", [aaa uppercaseString]);
NSLog(@"%@", [aaa lowercaseString]);
NSLog(@"%@", aaa);
//NSMutableString
//capctiy 是一个预估的值,可以改变
//由于内存比较紧张,一般把capctiy设置成0,让字符串自己去判断大小,扩充容器大小
//创建一个新的字符串
NSMutableString *mString = [[NSMutableString alloc] initWithCapacity:0];
//拼接字符串
[mString appendString:@"taiyang"];
NSLog(@"%@", mString);
//插入字符
[mString insertString:@"aaa" atIndex:1];
NSLog(@"%@", mString);
//删除字符串
// NSRange r = {2, 3};
// [mString deleteCharactersInRange:r];
// || 等价
[mString deleteCharactersInRange
练习 :
// 1.截取字符串“20|http://www.baidu.com”中 “|” 前面和后面的字符串,并输出。
// NSString *str1 = @"20|http://www.baidu.com";
// 1
// NSString *str2 = [str1 substringWithRange:NSMakeRange(0, 2)];
// NSString *str3 = [str1 substringWithRange:NSMakeRange(3, 20)];
// NSLog(@"str2 = %@ str3 = %@", str2, str3);
//2
// NSString *str2 = [str1 substringFromIndex:3];
// NSLog(@"str2 = '%@'", str2);
// NSString *str3 = [str1 substringToIndex:2];
// NSLog(@"str3 = '%@'", str3);
// 2.将“文艺青年”改成“213青年”。
// NSString *str1 = @"文艺青年";
// NSString *str2 = [str1 stringByReplacingOccurrencesOfString:@"文艺" withString:@"213"];
// NSLog(@"%@", str2);
// 3.给定一个图片文件名,判断字符串中是否有后缀,如果有(如:以“.png”结尾),就替换成“jpg”;如果没有,就拼接”.jpg”
// NSString *str1 = @"taiyang";
// NSUInteger length = [str1 length];
// NSString *str2 = [str1 substringFromIndex:length - 4];
// if ([@".jpg" compare:str2] == 0) {
// NSLog(@"该字符串是以.jpg开头");
// }else if ([@".png" compare:str2] == 0){
// NSString *str3 = [str1 stringByReplacingOccurrencesOfString:@".png" withString:@".jpg"];
// NSLog(@"str3 = '%@'", str3);
// }else{
// NSString *str4 = [str1 stringByAppendingString:@".jpg"];
// NSLog(@"str4 = '%@'", str4);
// }
NSString NSMutableString的更多相关文章
- NSString&NSMutableString常用操作梳理(转)
作者:弦苦 授权本站转载. 上一篇梳理了NSArray&NSMutableArray常用操作,这次来梳理一下Objective-C中每天都要用到的字符串处理类——NSString. Objec ...
- 关于NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary
NSString,NSMutableString,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary 在 OC 中我们天天都要用,而我们要怎 ...
- NSString&NSMutableString常用操作梳理
http://www.cocoachina.com/ios/20150724/12722.html 上一篇梳理了NSArray&NSMutableArray常用操作,这次来梳理一下Object ...
- NSString NSMutableString copy mutableCopy retain weak strong整合
copy retain assign的差别在于对象属性的set方法 NSString 与 NSMutableString NSString是不可变字符串对象,这句话的意思,结合代码: #import ...
- [转] NSString / NSMutableString 字符串处理,常用代码
原文 : http://justcoding.iteye.com/blog/1405951 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString , ...
- 【转】 NSString / NSMutableString 字符串处理,常用代码 (实例)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
- NSString / NSMutableString 字符串处理,常用代码 (实例)
http://blog.csdn.net/likendsl/article/details/7417878 Objective-C 中核心处理字符串的类是 NSString 与 NSMutableSt ...
- Objective-C NSString/NSMutableString
创建于完成: 2018/02/05 总览: http://www.cnblogs.com/lancgg/p/8404975.html 字符串类 简介 字符码: Unicode NSString ...
- 转:用法总结:NSNumber、NSString、NSDate、NSCalendarDate、NSData(待续)
NSNumber + (NSNumber *)numberWithInt:(int)value; + (NSNumber *)numberWithDouble:(double)value; - (in ...
随机推荐
- How to Pronounce Ending T Clusters + Homophones — Baking!
How to Pronounce Ending T Clusters + Homophones — Baking! Share Tweet Share Tagged With: ARE Reducti ...
- 初始化centoS 相关
install aspnetcoremodule for iis https://docs.microsoft.com/en-us/aspnet/core/publishing/iis?tabs=as ...
- 向数据库添加学生信息。存放在REQUEST对象里
代码前几天已经发过了,但是程序一直还没运行出来,今天重新建立了一个数据库,才可以,下面补充上数据截图
- 吴裕雄 数据挖掘与分析案例实战(4)——python数据处理工具:Pandas
# 导入模块import pandas as pdimport numpy as np # 构造序列gdp1 = pd.Series([2.8,3.01,8.99,8.59,5.18])print(g ...
- pymongo的常用操作
环境:pymongo3.0.3,python3 以下是我整理的一些关于pymongo的操作,网上很多是用pymongo.Connecion()去连接数据库的,但是我这里连接一直提示没有这个包,如果大家 ...
- 解决Lightmap在PC上与ios和Android上表现不同的问题
Lightmap在PC上与android和ios的区别以及解决方法 1. 问题描述 相信很多人碰到过Lightmap的一些问题: 烘培好Lightmap之后,在PC上看起来相当给力,而打包成ios或 ...
- OC 线程操作 - GCD使用 - 栅栏函数
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //同步函数无需栅栏函数 //栅栏 ...
- MyEclipse2018.9.0设置全局编码
1.windows->Preferences打开"首选项"对话框,左侧导航,导航到general->Workspace 右侧Text file encoding,选择O ...
- Spring框架整合WEB解决配置文件加载多次的问题
1. 创建JavaWEB项目,引入Spring的开发包.编写具体的类和方法. * 环境搭建好后,启动服务器来测试项目,发送每访问一次都会加载一次配置文件,这样效率会非常非常慢!! 2. 解决上面的问题 ...
- 【转】Https内部机制基础知识
互联网权威机构 - CA 机构,又称为证书授权 (Certificate Authority) 机构,浏览器会内置这些"受信任的根证书颁发机构" (即 CA). 数字证书 提及 H ...