关于string.format() 转
string.format()函数用来生成具有特定格式的字符串,这个函数有两个参数,第一个参数为格式化串:由指示符和控制格式的字符组成。第二个参数是对应格式中每个代号的各种数据.
格式字符串可能包含以下的转义码:
%c - 接受一个数字, 并将其转化为ASCII码表中对应的字符
%d, %i - 接受一个数字并将其转化为有符号的整数格式
%o - 接受一个数字并将其转化为八进制数格式
%u - 接受一个数字并将其转化为无符号整数格式
%x - 接受一个数字并将其转化为十六进制数格式, 使用小写字母
%X - 接受一个数字并将其转化为十六进制数格式, 使用大写字母
%e - 接受一个数字并将其转化为科学记数法格式, 使用小写字母e
%E - 接受一个数字并将其转化为科学记数法格式, 使用大写字母E
%f - 接受一个数字并将其转化为浮点数格式
%g(%G) - 接受一个数字并将其转化为%e(%E, 对应%G)及%f中较短的一种格式
%q - 接受一个字符串并将其转化为可安全被Lua编译器读入的格式
%s - 接受一个字符串并按照给定的参数格式化该字符串
为进一步细化格式, 可以在%号后添加参数. 参数将以如下的顺序读入:
(1) 符号: 一个+号表示其后的数字转义符将让正数显示正号. 默认情况下只有负数显示符号.
(2) 占位符: 一个0, 在后面指定了字串宽度时占位用. 不填时的默认占位符是空格.
(3) 对齐标识: 在指定了字串宽度时, 默认为右对齐, 增加-号可以改为左对齐.
(4) 宽度数值
(5) 小数位数/字串裁切: 在宽度数值后增加的小数部分n, 若后接f(浮点数转义符, 如%6.3f)则设定该浮点数的小数只保留n位, 若后接s(字符串转义符, 如%5.3s)则设定该字符串只显示前n位.
在这些参数的后面则是上述所列的转义码类型(c, d, i, f, ...).
print(string.format("pi = %.4f", PI))
--> pi = 3.1416
d = 5; m = 11; y = 1990
print(string.format("%02d/%02d/%04d", d, m, y))
--> 05/11/1990
tag, title = "h1", "a title"
print(string.format("<%s>%s</%s>", tag, title, tag))
--> <h1>a title</h1>
第一个例子,%.4f代表小数点后面有4位小数的浮点数。第二个例子%02d代表以固定的两位显示十进制数,不足的前面补0。而%2d前面没有指定0,不足两位时会以空白补足。对于格式串部分指示符得详细描述清参考lua手册,或者参考C手册,因为Lua调用标准C的printf函数来实现最终的功能。
以下是一些例子
- print(string.format("%%c: %c", 83)) --输出S
- print(string.format("%+d", 17.0)) --输出+17
- print(string.format("%05d", 17) ) --输出00017
- print(string.format("%o", 17) ) --输出21
- print(string.format("%u", 3.14) ) --输出3
- print(string.format("%x", 13) ) --输出d
- print(string.format("%X", 13) ) --输出D
- print(string.format("%e", 1000) ) --输出1.000000e+03
- print(string.format("%E", 1000) ) --输出1.000000E+03
- print(string.format("%6.3f", 13) ) --输出13.000
- print(string.format("%q", "One\nTwo") ) --输出"One\
- print(string.format("%s", "monkey") ) --输出monkey
- print(string.format("%10s", "monkey") ) --输出monkey
- print(string.format("%5.3s", "monkey") ) --输出mon
- string.format("{'playid':'%d','commodityid':'%d','price':'%d','price_type':'%d','num':'%d','activity':'%d' ,'time':'%d'}",pPlayer:GetGUID(),nItemID,(paymoney/nItemNum),nMoneyType,nItemNum,1,os.time())
关于string.format() 转的更多相关文章
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- 【转】string.Format对C#字符串格式化
转自:http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) str ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- string.Format格式化用法详解
1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...
- Js实现string.format
经常需要动态拼接html字符串,想到用类似于.net的string.format函数比较好,于是找了下,stackoverflow的代码: if (!String.prototype.format) ...
- String.Format用法
http://blog.csdn.net/yohop/article/details/2534907 1.作为参数 名称 说明 Format(String, Object) 将指定的 Stri ...
- String.Format 格式说明
C#格式化数值结果表 字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0 ...
- C# String.Format格式化json字符串中包含"{" "}"报错问题
json.Append(String.Format("{\"total\":{0},\"row\":{1}}", lineCount, st ...
- JAVA字符串格式化-String.format()的使用
String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重载形式. form ...
- $是对string.Format的简化
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
随机推荐
- 按Enter键执行表单验证
document.onkeydown = function(evt){ var evt = window.event?window.event:evt; if (evt.keyCode==13) { ...
- Add Two Numbers LeetCode Java
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- ASP.NET获取客户端的相关信息
/// <summary> /// 获取远程浏览器端 IP 地址 /// </summary> /// <returns> ...
- js框架设计1.2对象扩展笔记
需要一个新的功能添加到我们的命名空间上.这方法在JS中被叫做extend或者mixin,若是遍历属性用一下1.1代码,则会遍历不出原型方法,所以1.2介绍的是mass Framework里的mix方法 ...
- Unity3D LuaComponent(基于ulua)
LuaComponent可以支持配一个需要执行在这个gameObject上的lua脚本,并且每个gameObject上的lua都是一个实例 using UnityEngine; using LuaIn ...
- HDU 3879 Base Station(最大权闭合子图)
经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...
- myeclipse eclipse 使用git插件访问github 的解决方案
具体的步骤很多帖子都有写,这里不再赘述,需要说明的一点,git插件的版本很重要,在官网上下载的最新版本在MyEclipse8.5,MyEclipse9.1,MyEclipse10上面都没有成功. 这里 ...
- 在编译命令行中添加 /D_SCL_SECURE_NO_DEPRECATE
问题:Add the option /D_SCL_SECURE_NO_DEPRECATE to the compilation command 解决方案:项目属性 –> 配置属性 –> C ...
- Hello world!(OC)
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { NSLog(@"Hello ...
- C# 先说IEnumerable,我们每天用的foreach你真的懂它吗?
原文: http://www.cnblogs.com/zhaopei/p/5769782.html