iOS之NSString
做了不少時(shí)間的iOS開(kāi)發(fā)了,現(xiàn)在在閱讀官方文檔,特意整理出來(lái),沒(méi)任何技術(shù)含量,純粹是筆記形式,希望對(duì)自己對(duì)大家有些幫助。
?
首先,要理解NSString需要學(xué)習(xí)下字符編碼(ASCii,Unicode,UTF8)的一些知識(shí);
可上維基百科搜索:http://zh.wikipedia.org/zh-cn/UTF-8
?
現(xiàn)在開(kāi)始分享iOS NSString的一些特性:
NSString編碼:NSString的本質(zhì)是基于Unicode編碼的字符組成的數(shù)組;
NSString創(chuàng)建:如果你是用這種方式創(chuàng)建的:那么temp對(duì)象是在編譯時(shí)被創(chuàng)建,并且在程序運(yùn)行時(shí)一直存在,永遠(yuǎn)不會(huì)被釋放;
NSString *temp = @"Contrafibularity";數(shù)據(jù)存儲(chǔ):NSString不是設(shè)計(jì)用來(lái)存儲(chǔ)任意序列的字節(jié)的,如果想要這功能,就用NSData;
生成C String:如果要用NSString產(chǎn)生一個(gè)C String,建議使用UTF8String方法;
const char *cString = [@"Hello, world" UTF8String];在iOS中,CString是基于UTF-8編碼的,注意:此時(shí)你得到的cString只是一個(gè)臨時(shí)對(duì)象,當(dāng)自動(dòng)釋放發(fā)生后,該對(duì)象會(huì)失效;如果要繼續(xù)使用,需要重新申請(qǐng)內(nèi)存并做拷貝;
從File和URL中讀寫NSString:
當(dāng)讀或?qū)懸粋€(gè)NSString時(shí),需要明確指定字符編碼,如果無(wú)法獲取到編碼信息,官方文檔建議如下:
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:是一個(gè)字符串掃描工具,非常好用,運(yùn)營(yíng)一個(gè)官網(wǎng)的Demo應(yīng)該就能做一些基本使用了,
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);} }?創(chuàng)建標(biāo)準(zhǔn)文件路徑:
NSString *path = @"/usr/bin/./grep"; NSString *standardizedPath = [path stringByStandardizingPath]; // standardizedPath: /usr/bin/grep 補(bǔ)全相對(duì)路徑: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操作:
文件名匹配搜索:假設(shè)路徑“~/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"
轉(zhuǎn)載于:https://www.cnblogs.com/mrfriday/p/3213518.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的iOS之NSString的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [旧博客]Python 第一次
- 下一篇: 放弃winform的窗体吧,改用html