Lua:特定长度的字符串

2024-02-25

local data = "here is a string"
local no = 12
foo = string.format("%50s %05d",data,no)
print(foo:len(),string.format("%q",foo))

defines foo作为特定长度的字符串

"                                  here is a string 00012"

然而,有没有一个easy获得方式

"here is a string                                   00012"

我知道,我可以填满字符串data有空格

while data:len() < 50 do data = data.." " end

添加减号以格式化字符串%-50s将文本左对齐:

foo = string.format("%-50s %05d","here is a string", 12)
print(foo:len(), foo)

Output:

56  here is a string                                   00012

允许的标志:

- : left align result inside field
+ : always prefix with a sign, using + if field positive
0 : left-fill with zeroes rather than spaces
(space) : If positive, put a space where the + would have been
# : Changes the behaviour of various formats, as follows:
  For octal conversion (o), prefixes the number with 0 - if necessary.
  For hex conversion (x), prefixes the number with 0x
  For hex conversion (X), prefixes the number with 0X
  For e, E and f formats, always show the decimal point.
  For g and G format, always show the decimal point, and do not truncate trailing zeroes.
  The option to 'always show the decimal point' would only apply if you had the precision set to 0.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Lua:特定长度的字符串 的相关文章

随机推荐

  • 适用于无需互联网连接的 Android 应用的 Firebase Crashlytics

    我有一个看起来像这样的设置 App WiFi but no Hardware internet WiFi with internet
  • 无法使用 firebase 控制台发送数据消息

    我正在使用 firebase 控制台 并且只能使用它发送通知消息 有没有办法使用相同的方式发送数据消息 Firebase 通知控制台只能用于发送通知消息 它不能用于发送数据消息 参见表中Firebase 文档中的消息类型 https fir
  • 多个 Spark 作业通过分区将镶木地板数据附加到同一基本路径

    我有多个作业想要并行执行 这些作业使用分区将每日数据附加到同一路径中 e g dataFrame write partitionBy eventDate category mode Append parquet s3 bucket save
  • Angular 6升级:debounceTime不是Subject的属性

    我正在尝试将我的应用程序从 Angular 5 升级到 Angular 6 我按照https update angular io https update angular io 至少我认为我做到了 错误是 Property debounce
  • 在node.js中,如果没有收到http请求的响应,你怎么知道?

    好的 在下面的示例中 我向服务器请求一些内容 如果返回响应 我会解析 JSON 并将数据添加到我的 mongodb 中 但是 如果没有返回响应 则显然不会触发任何事件 我如何为此添加超时 以便如果没有收到响应 那么我可以取消请求而不会引发任
  • 使用 cython 比 struct.pack 更快

    我正在努力做得比struct pack 以包装整数的具体情况为例 通过答案这个问题 https stackoverflow com questions 58960014 how to loop over a list in cython p
  • 类名中包含数字的堆栈跟踪 - 为什么?

    当 FinalizeOperation 运行时 在较大应用程序的生产中 public interface OperationFinalizerHook void onOperationFinalize Operation operation
  • 仅使用 MySQLi 获取一行

    如何使用 MySQLi 只获取一个 INDEXED 行 我目前正在这样做 row result gt fetch MYSQLI ASSOC row row 0 还有别的办法吗 我知道 mysqli fetch row 但它不返回关联数组 U
  • 如何使用 Java 将文件传递给 Jenkins

    我创建了一个参数化的 Jenkins 作业 我将变量从 Java 传递给它 这是Java final HttpClient client new HttpClient final PostMethod buildMethod new Pos
  • 如何在 Hive 中使用日期按周动态分区表

    有一个 结果 表 其中包含 ID 和日期列 创建表结果 Id int 日期字符串 行格式分隔字段以 结尾 存储为文本文件 Id Date 11 2012 04 06 12 2012 05 08 13 2013 02 10 14 2013 0
  • 获取核心数据关系中的对象数量

    我有一个带有食谱的表格视图 用户可以添加和删除配方 单击菜谱时 会推送另一个表格视图 并显示配料 同样 用户可以添加和删除成分 配方和成分之间存在 oneToMany 关系 我想显示配方表视图行中的成分数量 我知道如何在界面生成器中使用行进
  • 将运算符 new(sizeof(T) * N) 返回的内存视为数组

    在 C 中 可以使用以下方式分配动态数组malloc sizeof T N 然后使用指针算术获取此动态数组中位于 i 偏移量的元素 在 C 中 可以使用类似的方法operator new 在相同的方式malloc 然后放置新的 例如 可以在
  • StringTemplate 缩进在 String 内添加空格

    这显然是一个SSCCE 我有以下模板文件 xml value lt lt a b value b a gt gt 使用以下代码 private static final String VALUE alpha nbeta public sta
  • Julia 中的特征分解比 Mathematica 慢 5 倍?

    我是 Julia 的新手 主要在 Mathematica 工作 所以我可能会犯一些基本的错误 我尝试计算 Julia 计算随机矩阵的特征系统所花费的时间 发现它比 Mathematica 慢 5 6 倍 在朱莉娅 D 1000 rand 1
  • 如何将自定义 RESTful 路由添加到 Rails 应用程序?

    我正在读这两页 resources http api rubyonrails org classes ActionController Resources html M000522 Adding more RESTful actions h
  • Python WWW 宏

    我需要类似 iMacros for Python 的东西 如果有这样的东西那就太好了 browse to www google com type in input search query click button search list
  • Matlab FFT 和 FFTW

    我正在尝试使用 FFTW 和 Matlab 进行相同的 FFT 我使用 MEX 文件来检查 FFTW 是否良好 我认为我的一切都是正确的 但是 我从 FFTW 得到荒谬的价值观 在同一输入信号上多次运行 FFTW 代码时 我没有得到相同的结
  • 如何在单个端口上同时运行tcp和udp?

    我遇到过这样的情况 我必须一次在一个端口上运行 UDP 和 TCP 这是因为在我的应用程序中任何人都可以随时调用任何协议 所以我需要不断检查传入的请求并服务该请求 谁能帮我摆脱java中的这种情况吗 您无法检查请求是 TCP 还是 UDP
  • React-Redux-Thunk:操作不返回调度

    我正在使用 React Native 和 Redux thunk 中间件 我的问题是调度函数不返回对象 甚至不控制台 这是我的操作文件 function movieSelc movie return type types MOVIE SEL
  • Lua:特定长度的字符串

    local data here is a string local no 12 foo string format 50s 05d data no print foo len string format q foo defines foo作