字符串格式化占位符
这篇文章总结nsstring format 支持的格式占位符
格式化占位符
NSString 和 CFString 支持格式化占位符和IEEE printf占位符一样 如表1所示。
注意:可以使用“n$”位置定位符,例如%1$@, %2$s.
此方法经常用于国际化,用于本地化语言的顺序不同,需要调换参数位置。如:
中文:“do %s , %s” = "%2$s做%1$s";
英文:“do %s , %s” = “do %1$s , %2$s!”;
NSLocalizedString(@"do %s , %s", @"sth", @"please");
输出:中文:please做sth
英文:do sth, please!
占位符 |
说明 |
---|---|
|
Objective-C 对象,如果对象拥有descriptionWithLocale:方法则调用这个方法返回的值,否则的话输出description返回的值; CFTypeRef对象,输出CFCopyDescription的返回值 |
|
|
|
有符号的32位整型(int) |
|
无符号的32位整型 ( |
|
无符号的32位整型 ( |
|
无符号的32位整型 ( |
|
无符号的32位整型 ( |
|
64位双精度浮点型 ( |
|
64位双精度浮点型 ( |
|
64位双精度浮点型 ( |
|
64位双精度浮点型 ( |
|
64位双精度浮点型 ( |
|
8位无符号字符,NSLog以ASCII格式打印出,如果不是ASCII字符,使用\\ddd八进制或者Unicode十六进制\\udddd, 'd'是数字 |
|
16位无符号字符,NSLog以ASCII格式打印出,如果不是ASCII字符,使用\\ddd八进制或者Unicode十六进制\\udddd, 'd'是数字 |
|
以null结尾的8位Unicode字符串 |
|
以null结尾的16位Unicode字符串 |
|
void指针,以0x开头的十六进制格式输出 |
|
64位浮点数(double),以0x开头,在小数点前有一个十六进制,使用小写p的指数 |
|
64位浮点数(double),以0X开头,在小数点前有一个十六进制,使用大写p的指数 |
|
64位浮点数 |
Length modifier |
Description |
---|---|
|
Length modifier specifying that a following |
|
Length modifier specifying that a following |
|
Length modifier specifying that a following |
|
Length modifiers specifying that a following |
|
Length modifier specifying that a following |
|
Length modifier specifying that a following |
|
Length modifier specifying that a following |
|
Length modifier specifying that a following |