NSString常见用法
1、创建常量字符串
NSString *str = @"Hello World!"; 2、创建空字符串,给予赋值
NSString *str = [[NSString alloc] init];
str = @"Hello World!";
[str release]; 3、initWithString方法
NSString *str = [[NSString alloc] initWithString:@"Hello World!"];
[str release]; 4、用标准c创建字符串:initWithCString方法
char *Cstr = "This is a String!";
NSString *str = [[NSString alloc] initWithCString:Cstr];
[str release]; 5、创建格式化字符串:占位符%
int i = ;
int j = ;
NSString *str = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];
[str release]; 6、创建临时字符串
NSString *str = [NSString stringWithCString:"Hello World"]; 7、从文件创建字符串
NSString *path = [[NSBundlemainBundle] pathForResource:@"str.text"ofType:nil];
NSString *str = [[NSString alloc] initWithContentsOfFile:path];
NSLog(@"str:%@",str);
[str release]; 8、用字符串创建字符串,并写入到文件
NSString *str = [[NSString alloc] initWithString:@"This is a String!"];
NSString *path = @"str.text";
[str writeToFile: path atomically: YES];
[str release]; 、isEqualToString方法
NSString *str01 = @"This is a String!";
NSString *str02 = @"This is a String!";
BOOL result = [str01 isEqualToString:str02];
NSLog(@"result:%d",result); 10、compare方法(comparer返回的三种值)
47 //NSOrderedSame判断两者内容是否相同
NSString *str01 = @"This is a String!";
NSString *str02 = @"This is a String!";
BOOL result = [str01 compare:str02] == NSOrderedSame;
NSLog(@"result:%d",result); //NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)
NSString *str01 = @"This is a String!";
NSString *str02 = @"this is a String!";
BOOL result = [str01 compare:str02] == NSOrderedAscending;
NSLog(@"result:%d",result); //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
NSString *str01 = @"this is a String!";
NSString *str02 = @"This is a String!";
BOOL result = [str01 compare:str02] == NSOrderedDescending;
NSLog(@"result:%d",result); 11、不考虑大小写比较字符串
NSString *str01 = @"this is a String!";
NSString *str02 = @"This is a String!";
BOOL result = [str01 caseInsensitiveCompare:str02] == NSOrderedSame; //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,str02小于str01为真)
NSLog(@"result:%d",result); NSString *str01 = @"this is a String!";
NSString *str02 = @"This is a String!";
BOOL result = [str01 compare:str02 options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame;
NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。
NSLog(@"result:%d",result); 12、输出大写或者小写字符串
NSString *string1 = @"String"; NSString *string2 = @"String"; NSLog(@"string1:%@",[string1 uppercaseString]);//大写 NSLog(@"string2:%@",[string2 lowercaseString]);//小写 NSLog(@"string2:%@",[string2 capitalizedString]);//首字母大小 13、-rangeOfString: 查找字符串某处是否包含其它字符串
NSString *string1 = @"This is a string";
NSString *string2 = @"string";
NSRange range = [string1 rangeOfString:string2];
int location = range.location;
int leight = range.length;
NSString *str = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Location:%i,Leight:%i",location,leight]];
NSLog(@"str:%@",str);
[str release]; 14、-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringToIndex:];
NSLog(@"string2:%@",string2); 15、-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringFromIndex:];
NSLog(@"string2:%@",string2); 16、-substringWithRange: 按照所给出的位置,长度,任意地从字符串中截取子串
NSString *string1 = @"This is a string";
NSString *string2 = [string1 substringWithRange:NSMakeRange(, )];
NSLog(@"string2:%@",string2); 17、-stringWithCapacity: 按照固定长度生成空字符串
NSMutableString *String;
String = [NSMutableString stringWithCapacity:]; 18、-appendString: and -appendFormat: 把一个字符串接在另一个字符串的末尾
NSMutableString *str1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [str1 appendString:@", I will be adding some character"]; [str1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]]; NSLog(@"str1:%@",str1); 19、-insertString: atIndex: 在指定位置插入字符串
NSMutableString *str1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [str1 insertString:@"Hi! " atIndex:]; NSLog(@"str1:%@",str1);
20、-setString:
NSMutableString *str = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [str setString:@"Hello Word!"]; NSLog(@"str:%@",str); 21、-replaceCharactersInRange: withString: 用指定字符串替换字符串中某指定位置、长度的字符串
NSMutableString *str = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"]; [str replaceCharactersInRange:NSMakeRange(, ) withString:@"That"]; NSLog(@"str:%@",str); 22、-hasPrefix: 检查字符串是否以另一个字符串开头
NSString *str = @"NSStringInformation.txt"; [str hasPrefix:@"NSString"] = = ? NSLog(@"YES") : NSLog(@"NO"); [str hasSuffix:@".txt"] = = ? NSLog(@"YES") : NSLog(@"NO"); 23、扩展路径
NSString *Path = @"~/NSData.txt"; NSString *absolutePath = [Path stringByExpandingTildeInPath]; NSLog(@"absolutePath:%@",absolutePath); NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]); 24、文件扩展名
NSString *Path = @"~/NSData.txt"; NSLog(@"Extension:%@",[Path pathExtension]);
// 切割所有的参数
NSArray *array = [str componentsSeparatedByString:@"&"];
NSString常见用法的更多相关文章
- NSString常见用法总结
//====================NSStirng 的常见用法==================== -(void)testString { //创建格式化字符串:占位符(由一个%加一个字 ...
- iOS 开发多线程篇—GCD的常见用法
iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...
- iOS开发多线程篇—GCD的常见用法
iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...
- [HMLY]9.深入浅出-iOS Reactive Cocoa的常见用法
简介 今天的主角是Reactive Cocoa,聊聊Reactive Cocoa的常见使用:KVO.Target.Delegate.Notification. Reactive Cocoa 是一个重量 ...
- iOS开发多线程篇 08 —GCD的常见用法
iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...
- iOS-Reactive Cocoa的常见用法
今天是周末,临近年底,工作上遇到不可抗力,会有点一些变动!这多少会让人有一点静不下来,但需克制,Reactive Cocoa是今天的主角! 废话不多说,今天聊聊Reactive Cocoa的常见使用! ...
- Linux中find常见用法
Linux中find常见用法示例 ·find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数 ...
- php中的curl使用入门教程和常见用法实例
摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...
- Guava中Predicate的常见用法
Guava中Predicate的常见用法 1. Predicate基本用法 guava提供了许多利用Functions和Predicates来操作Collections的工具,一般在 Iterabl ...
随机推荐
- Lua 是一个小巧的脚本语言
Redis进阶实践之七Redis和Lua初步整合使用 一.引言 Redis学了一段时间了,基本的东西都没问题了.从今天开始讲写一些redis和lua脚本的相关的东西,lua这个脚本是一个好东西,可以运 ...
- 软考之路--从生活着手,看PV怎样操作
PV操作.是软考其中一个非常重要的考点,一听到这个名词,顿时赶脚高大上有么有,在软考的历年试题中,也不乏PV操作的身影,老师也对PV操作进行了一次讲课,那时年少.听得稀里糊涂,也不是非常理解,在小编的 ...
- 使用 Bluemix™ Live Sync 高速更新 Bluemix 上执行的应用程序实例
假设您要构建 Node.js 应用程序,那么能够使用 IBM® Bluemix® Live Sync 高速更新 Bluemix 上的应用程序实例,并像在桌面上进行操作一样进行开发,而无需又一次部署.执 ...
- Poj1734题解
题目大意 求一个无向图的最小环 题解 假设是有向图的话.仅仅须要令f[i][i]=+∞,再floyd就可以: 对无向图.应该在floyd算法循环至k的一開始进行例如以下操作: 枚举i和j,假设点i存在 ...
- 初步使用RecyclerView实现瀑布流
先看效果 关于RecyclerView,真的是很强大. 个人觉得主要方便的地方是 1.直接可以设置条目布局,通过setLayoutManager LinearLayoutManager:线性布局,横向 ...
- Linux下SPI读写外部寄存器的操作
SPI写寄存器操作: staticvoid mcp251x_write_reg(struct spi_device *spi, uint8_t reg, uint8_t val) { stru ...
- 1.19 Python基础知识 - 软件目录开发规范及不同模块之间的调用
一个软件项目的开发,除了需要很厉害的开发能力,同时在软件开发项目时,也需要对项目结构有良好的组织能力,将功能进行拆分,不同的功能放在不同的目录或文件中,方便日后的维护,升级等操作.比如核心代码的目录, ...
- SVN和Git代码管理小结
SVN和Git代码管理小结 之前,先用的是SVN,后来用了Git,最近又在用SVN. 关于代码管理,写几句. 由于自己参与的项目,人通常不超过10个人,版本不是很多,协作比较正常,感觉SVN ...
- 【Codeforces Round #453 (Div. 2) B】Coloring a Tree
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 从根节点开始. 显然它是什么颜色.就要改成对应的颜色.(如果上面已经有某个点传了值就不用改 然后往下传值. [代码] #includ ...
- Windows(x86,64bit)升级MySQL 5.7.17免安装版的详细教程
MySQL需要升级到5.5.3以上版本才支持Laravel 5.4默认的utf8mb64字符编码.因此就把MySQL升级了一下,期间还是遇到些小问题,记录一下以供参考. 升级准备 备份之前MySql目 ...