Printing to string from stream

Hi,
I am trying to get the output of a stream into a string. For example
the highlight function outputs the result to an IO object, so I can open a file and write to it. I can also get it printed using stdout.

However, I just need to store it in a string. Can someone please let me know how to accomplish this.

using Highlights
highlight(stdout, MIME("text/html"), "a = 1+2", Lexers.JuliaLexer)

# I need to store the result in my_string however I cannot figure out how to get this to work
my_string  = highlight(something_here, MIME("text/html"), "a = 1+2", Lexers.JuliaLexer)

Perhaps use IOBuffer

https://docs.julialang.org/en/latest/base/io-network/#Base.IOBuffer

io = IOBuffer()
highlight(io, MIME("text/html"), "a = 1+2", Lexers.JuliaLexer)
my_string = String(take!(io))
2 Likes