Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Lua:特定长度的字符串_String_Lua_String Length - Fatal编程技术网

String Lua:特定长度的字符串

String Lua:特定长度的字符串,string,lua,string-length,String,Lua,String Length,将foo定义为特定长度的字符串 local data = "here is a string" local no = 12 foo = string.format("%50s %05d",data,no) print(foo:len(),string.format("%q",foo)) 然而,有没有一个简单的方法可以得到 " here is a string 00012" 我知道,我可以用空格填充字符串data "here i

foo
定义为特定长度的字符串

local data = "here is a string"
local no = 12
foo = string.format("%50s %05d",data,no)
print(foo:len(),string.format("%q",foo))
然而,有没有一个简单的方法可以得到

"                                  here is a string 00012"
我知道,我可以用空格填充字符串
data

"here is a string                                   00012"
while data:len()<50 do data=data..”结束

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

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

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

56  here is a string                                   00012

在格式中添加负号:
%-50s
嗯,很简单。。。thankshow用“-”替换空格?@wsha还有另一种方法:
foo=data。。string.rep(“-”,50-data:len())。。字符串格式(“%05d”,否)
- : 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.