- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
遵循 WITH-OUTPUT-TO-STRING 的文档和 GET-OUTPUT-STREAM-STRING ,我希望以下方法有效,而且它们确实有效:
(print
(with-output-to-string (sb nil)
(format sb "~A " "hello, ")
(format sb "~A~&" "world")
sb))
(print
(let ((sb (make-string-output-stream)))
(format sb "~A " "hello, ")
(format sb "~A~&" "world")
(get-output-stream-string sb)))
但是,下面的例子接近于其中一个例子 WITH-OUTPUT-TO-STRING , 不:
(print
(with-output-to-string (sb (make-array
'(0)
:element-type 'base-char
:fill-pointer 0
:adjustable t))
(format sb "~A " "hello, ")
(format sb "~A~&" "world")
sb))
相反,生成输出流本身,而不是其中包含的字符串:
#<SB-IMPL::FILL-POINTER-OUTPUT-STREAM {1005FBE523}>
我一直无法找到一种方法来提取输出中的字符串溪流。我怀疑这与动态范围有关,但我的理解步履蹒跚,在这里。
显然,我有办法达到预期的效果,所以我只是很想发现我对这门语言的误解。
因为文档说结果未定义GET-OUTPUT-STREAM-STRING 在由 MAKE-STRING-OUTPUT-STREAM 创建的流上不是,我以下内容不起作用并不奇怪:
(print
(with-output-to-string (sb (make-array
'(0)
:element-type 'base-char
:fill-pointer 0
:adjustable t))
(format sb "~A " "hello, ")
(format sb "~A~&" "world")
(get-output-stream-string sb)))
但我仍然很感激能在我的第三个文件中找到一种提取字符串的方法示例。
最佳答案
请注意 WITH-OUTPUT-TO-STRING以两种不同的方式返回值:
NIL
,那么它会创建一个字符串并返回它。您的代码:
(print
(with-output-to-string (sb (make-array ; creates a string
'(0)
:element-type 'base-char
:fill-pointer 0
:adjustable t))
(format sb "~A " "hello, ")
(format sb "~A~&" "world")
sb) ; you return the stream (which makes not much sense), but not the string
)
您已通过调用 MAKE-ARRAY
创建了一个字符串。就在那里。用它。为此,您通常需要将其绑定(bind)到某个变量。
返回字符串的例子:
(let ((s (make-array '(0)
:element-type 'base-char
:fill-pointer 0
:adjustable t)))
(with-output-to-string (sb s)
(format sb "~A " "hello, ")
(format sb "~A~&" "world"))
s)
或
(let ((s (make-array '(0)
:element-type 'base-char
:fill-pointer 0
:adjustable t)))
(with-output-to-string (sb s)
(format sb "~A " "hello, ")
(format sb "~A~&" "world")
s))
在最后一种情况下,with-output-to-string
返回值,因为它得到了一个用作输出目标的字符串。
关于common-lisp - 惊喜打印到字符串(普通的 lisp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35964160/
从 Python 3.6 开始,常规的字典会记住其插入的顺序:就是说,当遍历字典时,你获得字典中元素的顺序跟它们插入时的顺序相同。 在 3.6 之前,字典是无序的:遍历顺序是随机的。 关于有序字
我想创建一个非常严格的安全管理器,所以我扩展了 SecurityManager 并覆盖了所有自定义的 checkXXX 方法。 但后来我发现我的安全管理器没用,因为任何人都可以: System.set
所以我已经工作了一段时间了,似乎无法弄清楚这个该死的 ListView 业务..我让它在另一个应用程序中工作,但是当我复制粘贴时(我知道)它不起作用。所以在逛了大约6个小时后我决定来这里。 此代码执行
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我注意到 bash shell 可以为您的命令建议命令行开关。 只需键入您的命令、破折号 (-) 和制表符。 shell 将向您显示可用的开关。 例如,尝试: andy@andyx:~$ java -
这个问题在这里已经有了答案: How can I initialize a dictionary of distinct empty lists in Python? (7 个回答) 关闭2年前。 我
一个好熟人和前同事突然给我打电话,并给了我一份我真的不能忽视的契约(Contract)。他坚信我会很快掌握并掌握 C#(过去在 -> c/c++/PHP/Python/Lua 方面的经验)。 这个问题
我想在我的 Objective-C iPhone 应用程序中使用一些 C++ STL 集合。显然这可以通过给文件扩展名 ".mm"来实现。与此相关的怪癖/惊喜有哪些? 我想使用我熟悉的基本容器( ve
我正在尝试使用 scikit-surprise 模块来构建推荐系统,但是在编译它时遇到错误。 我收到 ImportError:无法导入名称“Reader”错误 我的类(class)如下 import
我是一名优秀的程序员,十分优秀!