gpt4 book ai didi

二郎 - io :format 's result/(formatting with io_lib:format/2)

转载 作者:行者123 更新时间:2023-12-01 10:58:40 25 4
gpt4 key购买 nike

我正在尝试获取 io:format/1 的输出结果。

我知道io_lib中也有类似的函数,io_lib:format/2,但是输出不一样。事实上,它根本没有做任何事情。
如果我尝试绑定(bind) io:format,则 ok 是有界的,并且格式化的字符串会被写入控制台。

所以我的问题是,如何使用 io_lib:format/2 获得相同的输出?
或者如何将格式化字符串绑定(bind)到变量?

1> A = io:get_line('> ').
> "test".
"\"test\".\n"
2> io:format(A).
"test".
ok
3> B = io_lib:format(A, []).
"\"test\".\n"
4> B.
"\"test\".\n"
5> C = io:format(A).
"test".
ok
6> C.
ok

最佳答案

io_lib:format不是输出函数 io:format是。而是 io_lib:format只返回值,但不输出。
io:format 的结果您将其视为“测试”。是发送到终端的渲染版本(包括换行符),然后返回 ok .反之,io_lib:format 的返回值你看到的"\"test\".\n"只是 erlang shell 对同一字符串的表示,引号和换行符被转义,并被自己的引号包围。
io_lib:format更常用于向字符串中插入值(类似于 C 的 printf 函数)。例如,做类似的事情

NewString = io_lib:format("The string entered was ~s I hope you like it",[A])
NewString 的值将会
The string entered was "test".
I hope you like it

Erlang Shell 的表示将是:
"The string entered was \"test\".\n I hope you like it"

如果您只想输出刚刚输入的值,那么 io:format足以满足您的需求。

关于二郎 - io :format 's result/(formatting with io_lib:format/2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13129304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com