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常见用法的更多相关文章

  1. NSString常见用法总结

    //====================NSStirng 的常见用法==================== -(void)testString { //创建格式化字符串:占位符(由一个%加一个字 ...

  2. iOS 开发多线程篇—GCD的常见用法

    iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...

  3. iOS开发多线程篇—GCD的常见用法

    iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...

  4. [HMLY]9.深入浅出-iOS Reactive Cocoa的常见用法

    简介 今天的主角是Reactive Cocoa,聊聊Reactive Cocoa的常见使用:KVO.Target.Delegate.Notification. Reactive Cocoa 是一个重量 ...

  5. iOS开发多线程篇 08 —GCD的常见用法

    iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...

  6. iOS-Reactive Cocoa的常见用法

    今天是周末,临近年底,工作上遇到不可抗力,会有点一些变动!这多少会让人有一点静不下来,但需克制,Reactive Cocoa是今天的主角! 废话不多说,今天聊聊Reactive Cocoa的常见使用! ...

  7. Linux中find常见用法

    Linux中find常见用法示例 ·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \; find命令的参数 ...

  8. php中的curl使用入门教程和常见用法实例

    摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...

  9. Guava中Predicate的常见用法

    Guava中Predicate的常见用法 1.  Predicate基本用法 guava提供了许多利用Functions和Predicates来操作Collections的工具,一般在 Iterabl ...

随机推荐

  1. js关于循环的理解

    学习任何语言都离不开循环,js也是一样,看了网上的资料,整理一份关于js循环的理解. 1.最基础循环,js和其他高级语言一样使用for.while循环 (function() { for(var i= ...

  2. ubuntu下eclipse java ee首次打开提示找不到jdk的问题

    最近想搭建一个本地服务器,方便写一些网络请求相关的demo,遂下载了eclipse  ee版 ( IDEA证书好贵,暂时不想买-=-),下载---解压 一切正常,但是当在terminal下打开ecli ...

  3. Elasticsearch之REST

    REST 简介-定义 REST (REpresentation State Transfer)描述了一个架构样式的网络系统,比如 web 应用程序.它首次出现在 2000 年 Roy Fielding ...

  4. Stacked Autoencoders

    转自:http://www.cnblogs.com/tornadomeet/archive/2013/03/25/2980357.html 如果使用多层神经网络的话,那么将可以得到对输入更复杂的函数表 ...

  5. 如何监控和解决SQL Server的阻塞(1) (当前阻塞)

    1. 什么是"阻塞"? 阻塞是SQL数据库应用"锁"机制的一个副作用.当一个应用请求针对某个数据库对象(例如全表,某行数据, 或者是某个数据页)加锁后,那么这个 ...

  6. OA项目笔记

    一.创建项目构架 1.创建一个Maven的web工程 1.1修改编译器版本 <properties> <project.build.sourceEncoding>UTF-8&l ...

  7. STM32上使用JSON

    一.STM32工程中添加JSON 最近在一网2串项目,串口和网口之间可能需要定义一下简单的通信协议,而通信协议上则需要去定义一下通信的数据格式,上次听剑锋说要用Json来定义,目前查了下资料具体如何去 ...

  8. 【习题 8-8 UVA - 1612】Guess

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] double千万不要用==判断相等... 而且两个保留2位有效数字的数字x,y 判断它们相等应该这样. int temp1 = ro ...

  9. Windows学习总结(2)——30+ Windows命令提示符快捷键汇总

    即便你平时经常用到 Windows 命令提示符,可能也会对本文将提到的快捷键数量感到惊讶.其实我们可以使用快捷键来简化命令提示符中的选择操作,或对文本进行重复操作,下面我们会列出完整列表. 大家都知道 ...

  10. 软考之路--从生活着手,看PV怎样操作

    PV操作.是软考其中一个非常重要的考点,一听到这个名词,顿时赶脚高大上有么有,在软考的历年试题中,也不乏PV操作的身影,老师也对PV操作进行了一次讲课,那时年少.听得稀里糊涂,也不是非常理解,在小编的 ...