# Accumulating strings dynamically

**URL:** https://discourse.julialang.org/t/accumulating-strings-dynamically/51209
**Category:** General Usage
**Created:** [December 3, 2020, 4:42pm UTC](https://discourse.julialang.org/t/accumulating-strings-dynamically/51209 "2020-12-03T16:42:04Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [December 3, 2020, 4:42pm UTC](https://discourse.julialang.org/t/accumulating-strings-dynamically/51209/1 "2020-12-03T16:42:04Z")

</div>

I have to dynamically accumulate strings and for each string simply accumulate characters in an array

```julia
push!(word, c)

```

and then using this nifty one-liner to convert to a string

```julia
foldl(*, word)

```

I’m just wondering if that’s the best way to solve the problem ?

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [December 3, 2020, 4:46pm UTC](https://discourse.julialang.org/t/accumulating-strings-dynamically/51209/2 "2020-12-03T16:46:14Z")

</div>

```julia
julia> word = ['a','b','c'];

julia> join(word)
"abc"

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 3, 2020, 4:46pm UTC](https://discourse.julialang.org/t/accumulating-strings-dynamically/51209/3 "2020-12-03T16:46:43Z")

</div>

No, write to a buffer: create `buf = IOBuffer()`, accumulate characters as `print(buf, c)`, and then do `String(take!(buf))` at the end.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [December 3, 2020, 4:48pm UTC](https://discourse.julialang.org/t/accumulating-strings-dynamically/51209/4 "2020-12-03T16:48:54Z")

</div>

> [@stevengj](#):
>
> No, write to a buffer: create `buf = IOBuffer()` , write characters as `print(buf, c)` , and then do `String(take!(buf))` at the end.

To be clear, this is exactly what `join` does internally, right? And the advantage of this over using `join` is just that you also skip the unnecessary collection into a `Vector{Char}` ?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 3, 2020, 5:24pm UTC](https://discourse.julialang.org/t/accumulating-strings-dynamically/51209/5 "2020-12-03T17:24:46Z")

</div>

> [@rdeits](#):
>
> And the advantage of this over using `join` is just that you also skip the unnecessary collection into a `Vector{Char}` ?

Yes.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [December 3, 2020, 5:34pm UTC](https://discourse.julialang.org/t/accumulating-strings-dynamically/51209/6 "2020-12-03T17:34:34Z")

</div>

@stevengj is the winner ! 🙂  
@yha comes in second place

and I came in last ☹

The foldl method is slowest because it does the most allocations. What’s particularly interesting is that the IOBuffer method uses about 1/3 the allocations of using join. And join uses about 1/2 the allocations of foldl.

Thanks everyone
