如何在erlang中格式化带有整数的扁平字符串? [英] How to format a flat string with integers in it in erlang?
问题描述
在erlang中,我想格式化其中包含整数的字符串,并且希望将结果展平.但我明白了:
In erlang, I want to format a string with integers in it and I want the result to be flattened. But I get this:
io_lib:format("sdfsdf ~B", [12312]).
[115,100,102,115,100,102,32,"12312"]
通过使用下面的代码,我可以得到所需的结果,但它确实不是很优雅.
I can get the desired result by using the code below but it is really not elegant.
lists:flatten(io_lib:format("sdfsdf ~B", [12312])).
"sdfsdf 12312"
是否存在更好的格式设置为整数的字符串,以便它们是扁平的?理想情况下,仅使用一个功能?
Is there a better formatting strings with integers in them, so that they are flat? Ideally, using only one function?
推荐答案
您可以像在示例中一样使用 lists:flatten/1
来扁平化列表.
You flatten a list using lists:flatten/1
as you've done in your example.
如果您可以接受二进制文件,那么 list_to_binary/1
会非常有效:
If you can accept a binary, list_to_binary/1
is quite efficient:
1> list_to_binary(io_lib:format("sdfsdf ~B", [12312])).
<<"sdfsdf 12312">>
但是,请问为什么首先需要一个固定清单.如果只是化妆品,则不需要它. io:format/1,2,3
和大多数其他端口功能( gen_tcp
等)接受所谓的深层IO列表(带有字符和二进制文件的嵌套列表):
However, question why you need a flat list in the first place. If it is just cosmetics, you don't need it. io:format/1,2,3
and most other port functions (gen_tcp
etc) accept so called deep IO lists (nested lists with characters and binaries):
2> io:format([115,100,102,115,100,102,32,"12312"]).
sdfsdf 12312ok
这篇关于如何在erlang中格式化带有整数的扁平字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!