# Converting a Array of strings to an Array of Char

**URL:** https://discourse.julialang.org/t/converting-a-array-of-strings-to-an-array-of-char/35123
**Category:** New to Julia
**Tags:** question
**Created:** [February 25, 2020, 4:51pm UTC](https://discourse.julialang.org/t/converting-a-array-of-strings-to-an-array-of-char/35123 "2020-02-25T16:51:35Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![floriano](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@floriano](https://discourse.julialang.org/u/floriano)
#### Post date: [February 25, 2020, 4:51pm UTC](https://discourse.julialang.org/t/converting-a-array-of-strings-to-an-array-of-char/35123/1 "2020-02-25T16:51:35Z")

</div>

I have an input similar like this:

```julia
a = ["123"; "456"; "abc"]
display(a)

3-element Array{String,1}:
 "123"
 "456"
 "abc"

```

I want to convert it to something like this:

```julia
3×3 Array{Char,2}:
 '1' '2' '3'
 '4' '5' '6'
 'a' 'b' 'c'

```

I have tried with split(), Vector() and a few other funny ideas, but I always get much more complicated Arrays. Is there an easy and computational unexpensive way to do that?  
I appreciate any hint.  
Thanks

---

<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: [February 25, 2020, 5:00pm UTC](https://discourse.julialang.org/t/converting-a-array-of-strings-to-an-array-of-char/35123/2 "2020-02-25T17:00:40Z")

</div>

Here’s a reasonably efficient approach:

```julia
julia> reduce(vcat, permutedims.(collect.(a)))
3×3 Array{Char,2}:
 '1' '2' '3'
 '4' '5' '6'
 'a' 'b' 'c'

```

Explanation:

- `collect` takes in a string and gives us a vector of `Char`s. Calling `collect.(a)` applies `collect` to each element:

```julia
julia> collect.(a)
3-element Array{Array{Char,1},1}:
 ['1', '2', '3']
 ['4', '5', '6']
 ['a', 'b', 'c']

```

- Those vectors are 1-D, and thus nominally column vectors. We want rows, so we permute the dimensions of each vector to make it into a row vector.

```julia
julia> permutedims.(collect.(a))
3-element Array{Array{Char,2},1}:
 ['1' '2' '3']
 ['4' '5' '6']
 ['a' 'b' 'c']

```

- Finally, we collect the result by combining each row with `vcat`

```julia
julia> reduce(vcat, permutedims.(collect.(a)))
3×3 Array{Char,2}:
 '1' '2' '3'
 '4' '5' '6'
 'a' 'b' 'c'

```

This isn’t the most efficient possible approach because it allocates a new vector to hold the characters in each string, then throws that vector away after building the matrix. We can save some memory allocation by doing the `permutedims` and `collect` steps lazily:

```julia
julia> reduce(vcat, (permutedims(collect(s)) for s in a))
3×3 Array{Char,2}:
 '1' '2' '3'
 '4' '5' '6'
 'a' 'b' 'c'

```

If you’re still concerned about performance, use `BenchmarkTools.jl` to measure it for your application.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [February 25, 2020, 5:15pm UTC](https://discourse.julialang.org/t/converting-a-array-of-strings-to-an-array-of-char/35123/3 "2020-02-25T17:15:16Z")

</div>

Note that apart from the “nesting multiple functions approach” you can also always just write a bunch of simple loops:

```julia
function func(a)
    n = length(a[1])
    A = Matrix{Char}(undef,n,n) # preallocating the result
    for i in 1:length(a) # looping over all strings
        for (j, c) in enumerate(a[i]) # looping over all chars in a string
            A[i, j] = c
        end
    end
    return A
end

```

Since it’s Julia, it will be fast. On my machine I get

```julia
julia> using BenchmarkTools

julia> @btime func($a);
  45.804 ns (1 allocation: 128 bytes)

julia> @btime reduce(vcat, permutedims.(collect.($a)));
  322.870 ns (11 allocations: 816 bytes)

```

---

<div class="post-metadata">

### Author: ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)
#### Post date: [February 25, 2020, 5:37pm UTC](https://discourse.julialang.org/t/converting-a-array-of-strings-to-an-array-of-char/35123/4 "2020-02-25T17:37:11Z")

</div>

Nice explanations.

It’s a pity that you can’t write `[c for c in v, v in a]` or perhaps `[c for c in a[i], i in eachindex(a)]`. Because these go to `Iterators.product`, and it needs to know all the ranges before starting.

You can write this

```julia
[a[i][j] for i=1:length(first(a)), j=1:length(a)]

```

but it won’t behave well with non-ascii strings, like

```julia
a = ["123"; "456"; "ábc"]

```

---

<div class="post-metadata">

### Author: ![floriano](https://avatars.discourse-cdn.com/v4/letter/f/67e7ee/32.png) [@floriano](https://discourse.julialang.org/u/floriano)
#### Post date: [February 25, 2020, 10:06pm UTC](https://discourse.julialang.org/t/converting-a-array-of-strings-to-an-array-of-char/35123/5 "2020-02-25T22:06:42Z")

</div>

Thank you to you all,  
I have now plenty of new ideas to continue working on my small problem.  
Special thanks to @rdeits for the detailed explanations.
