String Programming Guide 1
NSString 和 NSMutableString 的基本用法和一些注意事项.
官方原文链接:String Programming Guide
目录:
String //简述 cocoa 框架中的 NSString 及 NSMutableString
Creating and Converting String Objects //NSString 及 NSMutableString 的对象创建方式及与不同编码字符串的操作(转换,导入,到处)
Formatting String Objects //嗯
String Format Specifiers //NSString 所支持的规范化输入输出字符
Reading Strings From and Writing Strings To Files and URLs //NSString 对象读写文件
Searching, Comparing, and Sorting String //字符串查找,截取,比较方法
Words, Paragraphs, and Line Breaks //分词分行分段方式
Characters and Grapheme Clusters //面向用户的字符编码方式
Character Sets //介绍NSCharacterSet对象, 创建自定义字符集
Scanners //介绍 Scanner 对象, 将 NSString 中的字符串转成数字
String Representations of File Paths // NSURL对象介绍及其和 NSString 之间的转换
Drawing Strings //NSString 在 NSView 里的绘制方法
相关推荐:
"Attributed String Programming Guide"
"Data Formatting Guide"
"Internationalization and Localization Guide"
"String Programming Guide for Core Foundation"
String
Unicode 长度: length
取指定位置的字符: characterAtIndex
Creating and Converting String Bojects
创建:
NSString *temp = @"Contrafibularity";//直接使用@符号 NSString *tempUnicode = @"こんにちは";//直接写 unicode 数据,或者可以直接\u \U写 unicode 码
String 常量只要创建了就可以在内存里存活到程序退出
直接用常亮比较也可以:
BOOL same = [@"comparison" isEqualToString:temp];
NSString from C Strings and Data
使用 C 字符串创建 NSString 的时候需要指定编码方式:
使用方法:[[NSString alloc]initWithCString:aCString encoding:aCEncoding]
char *utf8String = "utf8String Temp"; NSString *stringFromUTFString = [[NSString alloc]initWithUTF8String:utf8String]; char *macOSRomanEncodedString = "macOSRomanEncodedString Temp"; NSString *stringFromMORString = [[NSString alloc]initWithCString:macOSRomanEncodedString encoding:NSMacOSRomanStringEncoding]; NSDate *shiftJISData = [NSData new]; NSString *stringFromJISData = [[NSString alloc]initWithData:shiftJISData encoding:NSShiftJISStringEncoding];
一个例子: 把 UTF8编码的NSString 转成 ASCII 编码的NSData再转回 NSString
unichar ellipsis = 0x2026; NSString *theString = [NSString stringWithFormat:@"To be continued%C",ellipsis]; NSData *asciiData = [theString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *asciiString = [[NSString alloc]initWithData:asciiData encoding:NSASCIIStringEncoding]; NSLog(@"Original: %@ (length %ld)", theString,[theString length]); NSLog(@"Converted: %@ (length %ld)",asciiString, [asciiString length]); //OUTPUT: 2015-06-09 21:00:03.382 XYZPersonTest[781:49623] Original: To be continued… (length 16) //OUTPUT: 2015-06-09 21:00:03.383 XYZPersonTest[781:49623] Converted: To be continued... (length 18)//这是大号的点,但是这里显示不出来为啥捏
PS:如果单纯的存数据的话建议使用 NSData
Variable Strings
使用[aString stringByAppendingString:bString]用来合并 aString 和 bString字符串
或者直接使用
[NSString stringWithFormat:@"format",var1,var2,var3] 或 [[NSString alloc]initWithFormat:@"format",var1,var2,var3]
来合并 var1,var2,var3(var123可以不是 NSString)
NSNumber *var1 = @998; NSString *var2 = @"banana"; float var3 = 0.23; NSString *tempString1 = [NSString stringWithFormat:@"%0.2lf of %@ cost %d",var3,var2,var1.intValue]; //same to this: //NSString *tempString1 = [[NSString alloc]initWithFormat:@"%lf of %@ cost %d",var3,var2,var1.intVal NSLog(@"%@",tempString1); //OUTPUT: 2015-06-09 21:29:18.141 XYZPersonTest[813:56882] 0.23 of banana cost 998 NSString *tempString2 = [tempString1 stringByAppendingString:@", that's cheap."]; NSLog(@"%@",tempString2); //OUTPUT: 2015-06-09 21:30:15.481 XYZPersonTest[822:57156] 0.23 of banana cost 998, that's cheap.
Strings to Present to the User
在把 String 展示到前段的时候,要考虑语言本地化. 尽量避免直接把展示内容直接写死在代码里面
NSString *greeting = NSLocalizedStringFromTable(@"Hello", @"greeting to present in first launch panel", @"greetings"); NSLog(@"%@",greeting); //OUTPUT: 2015-06-09 21:40:09.001 XYZPersonTest[832:58805] Hello //不知道 table 怎么设置, 以后要用到再看看
Combining and Extracting Strings
合并的话同上
subString 可以使用substringToIndex, substringFromIndex 和 substringWithRange方法
分割成多个 string 可以使用 componentsSeparatedByString方法
NSString *source = @"123456789"; NSString *firstFour = [source substringToIndex:4]; NSLog(@"%@",firstFour); //OUTPUT:2015-06-09 23:04:01.604 XYZPersonTest[3162:88485] 1234 NSString *allButFirstThree = [source substringFromIndex:3]; NSLog(@"%@",allButFirstThree); //OUTPUT: 2015-06-09 23:04:53.094 XYZPersonTest[3170:88855] 456789 NSRange twoToSixRange = NSMakeRange(2, 4); NSString *twoToSix = [source substringWithRange:twoToSixRange]; NSLog(@"%@",twoToSix); //OUTPUT: 2015-06-09 23:06:13.604 XYZPersonTest[3178:89333] 3456 NSArray *split = [source componentsSeparatedByString:@"45"]; NSLog(@"%@",split); //OUTPUT: 2015-06-09 23:07:02.059 XYZPersonTest[3186:89558] (123,6789)
如果需要使用符号匹配来完成 extract, 请使用 Scanners
Getting C Strings
建议使用 UTF8String 来获取一个NSString实例的 C String, 这个方法返回一个 UTF8编码的字符常量(const char):
const char *cString = [source UTF8String]; NSLog(@"%s",cString); //OUTPUT: 2015-06-09 23:11:53.439 XYZPersonTest[3203:91083] 123456789
注意UTF8String 返回的 const char 只是被保存在一个临时对象里,如果需要保持该值需要把它复制到一个 buffer 里面
使用其他编码方式初始化或者取出 NSString 里面的值, 实现与 NSData 值的转换:
使用 initWithData: encoding: 和 dataUsingEncoding 方法, 请查看上面的 一个例子
小结:
Source | Creation Method | Extraction Method |
In code | @"..." compiler construct | N/A |
UTF8 encoding | stringWithUTF8String | UTF8String |
Unicode encoding | stringWithCharacters: length: | getCharacters: getCharacters:range: |
Arbitrary encoding | initWithData: encoding: | dataUsingEncoding: |
Existing strings | stringByAppendingString: stringByAppendingFormat: | N/A |
Format string | localizedStringWithFormat: initWithFormat:locale: | Use NSScanner |
Localized strings | NSLocalizedString and similar | N/A |
Formatting String Objects
Formatting Basics
NSString所支持的格式化字符串标准和 ANSI C 函数 printf() 是一样的, 还在这基础上加了%@表示任何对象.
...@"%@ is good",var1
如果var1对象有 descriptionWithLocale: 方法, NSString 就优先调用这个方法 [var1 descriptionWithLocale:], 然后将返回值替换到前面格式化字符串的对应位置. 否则就调用description方法
Localizing String Resource 会用本地化语言格式来处理传入参数
NSString *string1 = [NSString stringWithFormat:@"A string: %@, a float: %1.2f",@"string",34.2651]; NSLog(@"%@",string1); //OUTPUT: 2015-06-10 09:51:36.541 XYZPersonTest[628:23712] A string: string, a float: 34.27 NSNumber *number = @1234; NSDictionary *dictionary = @{@"date":[NSDate date]}; NSString *baseString = @"Base string."; NSString *string2 = [baseString stringByAppendingFormat:@" A number: %@, a dictionary: %@", number, dictionary]; NSLog(@"%@",string2); //OUTPUT: 2015-06-10 09:54:53.406 XYZPersonTest[641:24505] Base string. A number: 1234, a dictionary: {date = "2015-06-10 01:54:53 +0000";}
可以直接在 stringWithFormat 等方法里面使用非 ASCII 编码的字符:
NSString *s = [NSString stringWithFormat:@"Long %C dash",0x2014]; NSLog(@"%@",s); //OUTPUT: 2015-06-10 10:06:51.013 XYZPersonTest[665:26976] Long — dash
使用3-byte 的 UTF-8 (\xe2\x80\x94等于0x2014):
NSString *s2 = [NSString stringWithFormat:@"Long \xe2\x80\x94 dash"]; NSLog(@"%@",s2); //OUTPUT: 2015-06-10 10:08:20.337 XYZPersonTest[680:27556] Long — dash
NSLog and NSLogv
注意事项:
NSString *string4 = @"A contrived string %@"; NSLog(string4); //运行时报错退出 NSString *string5 = @"A contrived string %@"; NSLog(@"%@",string5); //OUTPUT: 2015-06-10 10:13:27.884 XYZPersonTest[696:29339] A contrived string %@