做了不少时间的iOS开发了,现在在阅读官方文档,特意整理出来,没任何技术含量,纯粹是笔记形式,希望对自己对大家有些帮助。

首先,要理解NSString需要学习下字符编码(ASCii,Unicode,UTF8)的一些知识;

可上维基百科搜索:http://zh.wikipedia.org/zh-cn/UTF-8

现在开始分享iOS NSString的一些特性:

NSString编码:NSString的本质是基于Unicode编码的字符组成的数组;

NSString创建:如果你是用这种方式创建的:那么temp对象是在编译时被创建,并且在程序运行时一直存在,永远不会被释放;

NSString *temp = @"Contrafibularity";

数据存储:NSString不是设计用来存储任意序列的字节的,如果想要这功能,就用NSData;

生成C String:如果要用NSString产生一个C String,建议使用UTF8String方法;

const char *cString = [@"Hello, world" UTF8String];

  在iOS中,CString是基于UTF-8编码的,注意:此时你得到的cString只是一个临时对象,当自动释放发生后,该对象会失效;如果要继续使用,需要重新申请内存并做拷贝;

从File和URL中读写NSString:

  当读或写一个NSString时,需要明确指定字符编码,如果无法获取到编码信息,官方文档建议如下:

1.Try stringWithContentsOfFile:usedEncoding:error: orinitWithContentsOfFile:usedEncoding:error: (or the URL-based equivalents).These methods try to determine the encoding of the resource, and if successful return by reference the encoding used.
2.If (1) fails, try to read the resource by specifying UTF-8 as the encoding.
3.If (2) fails, try an appropriate legacy encoding. "Appropriate" here depends a bit on circumstances;it might be the default C string encoding, it might be ISO or Windows Latin 1, or something else,  depending on where your data are coming from.
4.Finally, you can try NSAttributedString's loading methods from the Application Kit (such asinitWithURL:options:documentAttributes:error:).These methods attempt to load plain text files, and return the encoding used.They can be used on more-or-less arbitrary text documents, and are worth considering if your application has no special expertise in text.  They might not be as appropriate for Foundation-level tools or documents that are not natural-language text.

 NSScaner:是一个字符串扫描工具,非常好用,运营一个官网的Demo应该就能做一些基本使用了,

NSString *string = @"Product: Acme Potato Peeler; Cost: 0.98 73\n\Product: Chef Pierre Pasta Fork; Cost: 0.75 19\n\Product:Chef Pierre Colander; Cost: 1.27 2\n";
NSCharacterSet *semicolonSet;
NSScanner *theScanner;NSString *PRODUCT = @"Product:";
NSString *COST = @"Cost:";NSString *productName;
float productCost;NSInteger productSold;
semicolonSet = [NSCharacterSet characterSetWithCharactersInString:@";"];
theScanner = [NSScanner scannerWithString:string];while ([theScanner isAtEnd] == NO)
{if ([theScanner scanString:PRODUCT intoString:NULL] &&[theScanner scanUpToCharactersFromSet:semicolonSet intoString:&productName] &&[theScanner scanString:@";" intoString:NULL] &&[theScanner scanString:COST intoString:NULL] &&[theScanner scanFloat:&productCost] &&[theScanner scanInteger:&productSold]){NSLog(@"Sales of %@: $%1.2f", productName, productCost * productSold);}
} 

 创建标准文件路径:

NSString *path = @"/usr/bin/./grep";
NSString *standardizedPath = [path stringByStandardizingPath];
// standardizedPath: /usr/bin/grep

补全相对路径:
NSString *meHome = [@"~me" stringByExpandingTildeInPath];
// meHome = @"/Users/me"

获取Document目录:
NSString *documentsDirectory;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);if ([paths count] > 0) {documentsDirectory = [paths objectAtIndex:0];
}

路径Component操作:        文件名匹配搜索:假设路径“~/Demo/”下有如下文件ReadMe.txt readme.html readme.rtf recondite.txt test.txt 
NSString *partialPath = @"~/Demo/r";
NSString *longestCompletion;
NSArray *outputArray;unsigned allMatches = [partialPath completePathIntoString:&longestCompletioncaseSensitive:NOmatchesIntoArray:&outputArrayfilterTypes:NULL];// allMatches = 3
// longestCompletion = @"~/Demo/re"
// outputArray = (@"~/Demo/readme.html", "~/Demo/readme.rtf", "~/Demo/recondite.txt"

转载于:https://www.cnblogs.com/mrfriday/p/3213518.html

iOS之NSString相关推荐

  1. 【iOS】NSString rangeOfString

    今天遇到了 NSString 的 rangeOfString 方法,刚遇到的时候不知道什么作用, 网上找到了一篇文章,介绍得挺简洁,代码如下: NSString *str1 = @"can ...

  2. iOS截取NSString字符串

    NSString类中提供了这样三个方法用于获取子字符串: substringFromIndex: substringWithRange: substringToIndex: 具体的使用见下面代码即可知 ...

  3. iOS开发-NSString去掉所有换行及空格

    1.去掉字符串两端的空格及回车 - (NSString *)removeSpaceAndNewline:(NSString *)str{NSString *temp = [str stringByTr ...

  4. IOS 关于NSString类型的属性为什么有时用copy,有时用strong呢?

    对于很多初学者,发现在修饰NSString类型的对象时,会有这样的疑惑?怎么有些人用strong修饰,而有些人用copy修饰呢? 这里有个例子,一.首先声明2个属性: @property (nonat ...

  5. iOS中NSString转换成HEX(十六进制)-NSData转换成int

    NSString *str = @"0xff055008"; //先以16为参数告诉strtoul字符串参数表示16进制数字,然后使用0x%X转为数字类型 unsigned lon ...

  6. iOS学习 NSString常用技巧

    字符串是程序设计最常用的数据类型之一了.在Mac/iPhone编程中,苹果为我们提供了一个不同的字符串类型NSString.有别与普通的String为数据类型,NSString其实是一个对象类型.NS ...

  7. iOS 关于NSString的一些方法

    在项目中整理的一些关于字符串应用方法,可以全部封装在一个类里面进行调用,会不断更新添加: 1.数字转换成对应的中文数字(项目中课程分级目录的章节号用到) 摘自:http://blog.csdn.net ...

  8. ios 中NSString的一些调用

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {     @autoreleasepo ...

  9. IOS开发-Nsstring中搜索方法rangeOfString

    NSString *str1 = @"can you \n speak English"; NSString *str = @"\n"; //在str1这个字符 ...

最新文章

  1. linux命令大全 笔试,Linux基础及常用命令(笔试面试必备)
  2. 与自定义词典 分词_【201110】ElasticSearch实现中文分词查询
  3. electron sqlite3_electron集成sqlite3,win10上折腾了2天
  4. 安装openssl-devel命令
  5. 【小程序demo】带你玩转支付宝小程序之小程序二维码
  6. 吴恩达深度学习第三周
  7. hud android,HUD | F-Droid - Free and Open Source Android App Repository
  8. cad等比例缩放快捷键_CAD比例缩放怎么用,快捷键命令SCALE
  9. 中英文符号对应字母表
  10. ES6入门:模板字符串
  11. 编程求1平方+2平方+...+n平方
  12. 基于stm32的绘图机器人设计
  13. 【洛谷】P1138 第k小整数
  14. python word修改神器docxtpl
  15. 使用Python爬取豆瓣电影 Top 250
  16. USB Type-C简介
  17. vue mounted
  18. 低于90分的成绩 java_查询平均成绩低于60分的学生学号、姓名及成绩。
  19. 基于ESP32的智能家居控制系统-微信小程序
  20. pyspark的聚合函数agg使用

热门文章

  1. 精通Spring Boot——第十一篇:使用自定义配置
  2. 服务端架构中的“网关服务器”
  3. socket第三方库 AsyncSocket(GCDAsyncSocket)
  4. 关于java连接sqlserver2000 和sqlserver2005的初识
  5. STB 上Linux软件系统解决方案
  6. linux下tar解压特定的目录
  7. 联想ThinkPad笔记本广告向苹果MacBook Air开战
  8. 关于所谓的穷人富人幸福论
  9. wireshark使用教程 linux,Linux入门教程:ubuntu下安装wireshark(以及配置非root),这个强大的工具可以捕...
  10. JQuery 1.6+ checkbox 状态选择