How to redirect IO to a string?

For example, the built-in versioninfo() function accepts arguments

versioninfo(io::IO=stdout; verbose::Bool=false)

The return result of this function is nothing. How can I get the output not as IO printing but as a string? Can I create an object of type IO but secretly stores the result into a string?

1 Like

Use an IOBuffer:

buf = IOBuffer()
versioninfo(buf)
String(take!(buf))
3 Likes

See also sprint.

2 Likes