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设计模式--------基本概念的理解

    1.闭包,前面已经说过,这里不再做说明 2.封装    对于JS而言,他不像java一样存在私有,公有 ,可以让对象在一些细节方面存在差异,降低他们的耦合程度,对数据做一些约束,我们可以更容易调试,封 ...

  2. nginx最新配置

    #user  nobody;worker_processes  1; #error_log  logs/error.log;#error_log  logs/error.log  notice;#er ...

  3. 用了Redis里面的map和set

    map的操作用 hset,hget等 set的操作有 sadd sismember等 参考下面: http://blog.csdn.net/kwsy2008/article/details/48467 ...

  4. js31---观察者模式

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  5. HTTP协议建立连接、通讯与关闭连接全过程

    为解决服务器TimeWait多的问题,了解了一下TCP/IP协议的连接过程.以访问一静态页面为例,从建立连接到访问拿到数据,然后关闭的整个过程.使用EtherPeek截图如下:   图首为一次交互过程 ...

  6. BZOJ 3456 城市规划 ( NTT + 多项式求逆 )

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3456 题意: 求出\(n\)个点的简单(无重边无自环)无向连通图的个数.(\(n< ...

  7. Java 8 Stream Tutorial--转

    原文地址:http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/ This example-driven tutori ...

  8. 去掉“此电脑”中的“WPS云文档”图标

    平台:Win10 问题:安装了WPS2019专业版后,此电脑窗口出现了一个WPS云文档图标,无法删除,云文档设置中也无法取消. 解决:打开注册表,定位到HKEY_CURRENT_USER\Softwa ...

  9. 关于Webpack详述系列文章 (第二篇)

    1.缩小文件搜索范围 1.1.1 include & exclude module:{ rules:[ { test:/\.js$/, use:['babel-loader?cacheDire ...

  10. 【2017"百度之星"程序设计大赛 - 初赛(A)】小C的倍数问题

    [链接]http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=775&pid=1001 [题意] 在这里写题意 [题 ...