# Splitting an array of strings

**URL:** <https://discourse.julialang.org/t/splitting-an-array-of-strings/16634>\
**Category:** New to Julia\
**Created:** [October 22, 2018, 3:39pm UTC](https://discourse.julialang.org/t/splitting-an-array-of-strings/16634 "2018-10-22T15:39:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [October 22, 2018, 3:39pm UTC](https://discourse.julialang.org/t/splitting-an-array-of-strings/16634/1 "2018-10-22T15:39:39Z")

</div>

Hello,  
I am trying to split an array of strings so that each row of the string array is split into individual elements.

```julia
a = ["MARZ M AA1 R Z";
"MARZAN M AA1 R a = Z AH0 N";
"MARZANO M AA0 R Z AA1 N OW2";
"MARZEC M AA1 R Z IH0 K";
"MARZETTE M AA2 R Z EH1 T";
"MARZILLI M AA0 R Z IY1 L IY2";
"MARZO M AA1 R Z OW2";
"MARZOLF M AA1 R Z OW2 L F"]

```

I’ve tried this:

```julia
for i in a
    push!(a,split(i))
end

```

but I get this error:

```julia
ERROR: MethodError: Cannot `convert` an object of type Array{SubString{String},1} to an object of type String
This may have arisen from a call to the constructor String(...),
since type constructors fall back to convert methods.

```

Many thanks!  
Nakul

---

<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:** [October 22, 2018, 3:50pm UTC](https://discourse.julialang.org/t/splitting-an-array-of-strings/16634/2 "2018-10-22T15:50:44Z")

</div>

> [@Nakul\_Tiruviluamala](#):
>
> each row of the string array is split into individual elements.

is a bit hard to understand. Let’s say you have

```julia
a = ["ab ab", "cd cd cd"]

```

what do you want the output to be?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 22, 2018, 4:09pm UTC](https://discourse.julialang.org/t/splitting-an-array-of-strings/16634/3 "2018-10-22T16:09:24Z")

</div>

If I understand you correctly you are looking for

```julia
[split(el, " ") for el in a]

```

This returns a `Vector{Substring{String}}[8]`, where each element is one of the elements in `a` split on spaces.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [October 22, 2018, 4:33pm UTC](https://discourse.julialang.org/t/splitting-an-array-of-strings/16634/4 "2018-10-22T16:33:26Z")

</div>

Here’s some python code that parses this type of data. Perhaps you can get some inspiration from that, since you can implement it quite similarly in Julia.

[https://github.com/MycroftAI/mimic2/blob/master/text/cmudict.py](https://github.com/MycroftAI/mimic2/blob/master/text/cmudict.py)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 22, 2018, 4:37pm UTC](https://discourse.julialang.org/t/splitting-an-array-of-strings/16634/5 "2018-10-22T16:37:04Z")

</div>

A shorter way would be `split.(a)` - the default split character is already space.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 22, 2018, 5:33pm UTC](https://discourse.julialang.org/t/splitting-an-array-of-strings/16634/6 "2018-10-22T17:33:26Z")

</div>

If you want all strings to end up in one long array, you can

```julia
julia> String.(reduce(vcat,split.(a)))
58-element Array{String,1}:
 "MARZ"   
 "M"      
 "AA1"    
 "R"      
 "Z"      
 "MARZAN" 
 "M"      
 "AA1"    
 "R"      
 "a"      
 "="      
 "Z"      
 "AH0"    
 "N"      
 ⋮        

```

where `reduce(vcat,...` concates all Vectors of Strings and `String.()` converts from `SubString` to `String`
