# String builder in Julia?

**URL:** <https://discourse.julialang.org/t/string-builder-in-julia/17225>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 6, 2018, 9:30pm UTC](https://discourse.julialang.org/t/string-builder-in-julia/17225 "2018-11-06T21:30:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [November 6, 2018, 9:30pm UTC](https://discourse.julialang.org/t/string-builder-in-julia/17225/1 "2018-11-06T21:30:06Z")

</div>

Is there anything like a string builder in Julia? What is the recommended approach to build a string by iteratively concatenating parts to it?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 6, 2018, 9:43pm UTC](https://discourse.julialang.org/t/string-builder-in-julia/17225/2 "2018-11-06T21:43:23Z")

</div>

1. Create an `IOBuffer`, `buffer = IOBuffer()`.
2. Print to the buffer, `print(buffer, "Hello")`
3. Take from the buffer, `String(take!(buffer))`

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [November 6, 2018, 10:34pm UTC](https://discourse.julialang.org/t/string-builder-in-julia/17225/3 "2018-11-06T22:34:09Z")

</div>

And if you really need to squeeze out performance you can pass `sizehint` to `IOBuffer` to preallocate memory if you can estimate the size of target string.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [November 8, 2018, 2:40pm UTC](https://discourse.julialang.org/t/string-builder-in-julia/17225/4 "2018-11-08T14:40:59Z")

</div>

Does this approach provide better performance than `string(a, b)` or `a * b` or `"$a $b"`? Is it recommended when building relatively large strings, like say a web page?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 8, 2018, 2:50pm UTC](https://discourse.julialang.org/t/string-builder-in-julia/17225/5 "2018-11-08T14:50:37Z")

</div>

> [@essenciary](#):
>
> Does this approach provide better performance than `string(a, b)` or `a * b` or `"$a $b"` ?

`"$a $b"` is exactly the same as writing `string(a, " ", b)`.  
`string(args...)` writes its arguments to an `IOBuffer` and then take the resulting string from it (unless all `args` are strings because then the output length can be preallocated).

> [@essenciary](#):
>
> Is it recommended when building relatively large strings, like say a web page?

Yes

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [November 8, 2018, 2:51pm UTC](https://discourse.julialang.org/t/string-builder-in-julia/17225/6 "2018-11-08T14:51:51Z")

</div>

Nice, thanks!

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [November 8, 2018, 2:55pm UTC](https://discourse.julialang.org/t/string-builder-in-julia/17225/7 "2018-11-08T14:55:50Z")

</div>

> [@kristoffer.carlsson](#):
>
> unless all `args` are strings because then the output length can be preallocated

and because of this feature AFAIK e.g. `string(a, b, c)` will be significantly faster than `IOBuffer` solution (even with `sizehint`) if `a`, `b` and `c` are strings. The downside is that you need to have `a`, `b` and `c` before calling `string`. With `IOBuffer` you are fully dynamic (no need to have the strings upfront and no need to know how many of them you have), so this is a recommended solution for building complex strings.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [November 11, 2018, 8:39pm UTC](https://discourse.julialang.org/t/string-builder-in-julia/17225/8 "2018-11-11T20:39:11Z")

</div>

[https://github.com/davidanthoff/StringBuilders.jl](https://github.com/davidanthoff/StringBuilders.jl)

This is just a very thin wrapper around the `IOBuffer` suggestion from above. But I can never remember that, so this might make it slightly easier to use. In my mind it would make a good candidate to be moved to base at some point.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/julialang/original/3X/1/2/12829a7ba92b924d4ce81099cbf99785bee9b405.png) [@system](https://discourse.julialang.org/u/system)\
**Post date:** [November 23, 2018, 8:48pm UTC](https://discourse.julialang.org/t/string-builder-in-julia/17225/9 "2018-11-23T20:48:22Z")

</div>

This topic was automatically closed 12 days after the last reply. New replies are no longer allowed.
