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 ...
随机推荐
- vue-cli 搭建
一.安装vue-cli 安装vue-cli的前提是你已经安装了npm,安装npm你可以直接下载node的安装包进行安装.你可以在命令行工具里输入npm -v 检测你是否安装了npm和版本情况.出现版 ...
- C++ 补课(一)
1,在C语言中,全局变量必须声明在所有的函数之前,局部变量必须声明在所有可执行语句之前: C++ 允许在代码块的任何位置对局部变量进行声明 2,常量定义方面,C语言 #define 可能因计算的优先级 ...
- HTML 页面内容禁止选中
写一个小笔记,怎么禁止HTML页面不被选中,复制. CSS: *{ moz-user-select: -moz-none; -moz-user-select: none; -o-user-select ...
- 今日题解------codeforce 893d
题意:给你一个数列,小于零表示表示信用卡里取出钱,大于零表示信用卡里存钱,等于零表示要查询信用卡, 如果被查到信用卡里的钱小于零,那你就GG,或者在任何时候你的信用卡里的钱大于d的话(不需要找ai等于 ...
- 小的时候.by小雷
小的时候,总是有很多想法. 想去做,却做不成. 因为,自己小,被父母约束着,被学校圈着,被老师教育着. 想买个小霸王游戏机,没钱.在父辈的眼中,"游戏" ,游戏室,电脑游戏 ...
- 00084_Map接口
1.Map接口概述 通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它们存储数据的形式不同. (1)Collection中的集合,元素是孤立存在的(理解为单身),向集 ...
- 基本3D变换之World Transform, View Transform and Projection Transform
作者:i_dovelemon 来源:CSDN 日期:2014 / 9 / 28 主题:World Transform, View Transform , Projection Transform 引言 ...
- js33--责任链模式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- redmine-bug 问题修改流程
1.当我们接受到测试发过来的bug以后,首先就是针对描述,弄清出问题,如果不能重现或者是对问题不清楚,那么可以去找提出者确认 注意:有时候一个问题可能是经过几次重开或者追加提出的,这时候你一定要从头到 ...
- Spring中JDBCTemplate的入门
Spring是IOC和AOP的容器框架,一站式的框架 连接数据库的步骤:[必须会写] Spring当中如何配置连接数据库? 第一步配置核心配置文件: <?xml version="1. ...