字符串格式化占位符
2014-04-09 17:08
246 查看
字符串格式化占位符
这篇文章总结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的返回值 |
%% | 输出‘%’字符 |
%d, %D | 有符号的32位整型(int) |
%u, %U | 无符号的32位整型 (unsigned int). |
%x | 无符号的32位整型 (unsigned int), 以十六进制格式小写输出 |
%X | 无符号的32位整型 (unsigned int), 以十六进制格式大写输出 |
%o, %O | 无符号的32位整型 (unsigned int), 以八进制格式输出 |
%f | 64位双精度浮点型 (double). |
%e | 64位双精度浮点型 (double), 以指数格式使用小写e指数 |
%E | 64位双精度浮点型 (double), 以指数格式使用大写e指数 |
%g | 64位双精度浮点型 (double), 自动处理,如果指数小于-4或大于等于精度,按照%e格式输出,否则按照%f输出 |
%G | 64位双精度浮点型 (double), 自动处理,如果指数小于-4或大于等于精度,按照%E格式输出,否则按照%f输出 |
%c | 8位无符号字符,NSLog以ASCII格式打印出,如果不是ASCII字符,使用\\ddd八进制或者Unicode十六进制\\udddd, 'd'是数字 |
%C | 16位无符号字符,NSLog以ASCII格式打印出,如果不是ASCII字符,使用\\ddd八进制或者Unicode十六进制\\udddd, 'd'是数字 |
%s | 以null结尾的8位Unicode字符串 |
%S | 以null结尾的16位Unicode字符串 |
%p | void指针,以0x开头的十六进制格式输出 |
%a | 64位浮点数(double),以0x开头,在小数点前有一个十六进制,使用小写p的指数 |
%A | 64位浮点数(double),以0X开头,在小数点前有一个十六进制,使用大写p的指数 |
%F | 64位浮点数 |
Length modifier | Description |
---|---|
h | Length modifier specifying that a following d, o, u, x, or Xconversion specifier applies to a shortor unsigned shortargument. |
hh | Length modifier specifying that a following d, o, u, x, or Xconversion specifier applies to a signed charor unsigned charargument. |
l | Length modifier specifying that a following d, o, u, x, or Xconversion specifier applies to a longor unsigned longargument. |
ll, q | Length modifiers specifying that a following d, o, u, x, or Xconversion specifier applies to a long longor unsigned long longargument. |
L | Length modifier specifying that a following a, A, e, E, f, F, g, or Gconversion specifier applies to a long doubleargument. |
z | Length modifier specifying that a following d, o, u, x, or Xconversion specifier applies to a size_tor the corresponding signed integer type argument. |
t | Length modifier specifying that a following d, o, u, x, or Xconversion specifier applies to a ptrdiff_tor the corresponding unsigned integer type argument. |
j | Length modifier specifying that a following d, o, u, x, or Xconversion specifier applies to a intmax_tor uintmax_targument. |
相关文章推荐
- asp格式化日期时间格式的代码
- PHP echo,print,printf,sprintf函数之间的区别与用法详解
- javascript 格式化时间日期函数代码脚本之家修正版
- js对数字的格式化使用说明
- 关于mysql数据库格式化简单介绍
- php printf输出格式使用说明
- PHP学习之输出字符串(echo,print,printf,print_r和var_dump)
- 再谈DOS批处理下格式化日期字符串的多种方法(详解)
- php输出echo、print、print_r、printf、sprintf、var_dump的区别比较
- javascript 数字格式化输出的实现代码
- 实现批量linux格式化硬盘和挂载硬盘脚本分享
- C语言printf详细解析
- C#日期时间格式化小结
- JavaScript自定义日期格式化函数详细解析
- 在输入输出字符串时scanf(),printf()和gets(),puts()的区别浅谈
- php中sprintf与printf函数用法区别解析
- Js获取当前日期时间及格式化代码
- JavaScript 中Date对象的格式化代码方法汇总
- JavaScript 在线压缩和格式化收藏
- javascript日期对象格式化为字符串的实现方法