# Convert split result to array of strings

**URL:** <https://discourse.julialang.org/t/convert-split-result-to-array-of-strings/8508>\
**Category:** General Usage\
**Created:** [January 21, 2018, 7:32pm UTC](https://discourse.julialang.org/t/convert-split-result-to-array-of-strings/8508 "2018-01-21T19:32:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivo\_welch](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@ivo\_welch](https://discourse.julialang.org/u/ivo_welch)\
**Post date:** [January 21, 2018, 7:32pm UTC](https://discourse.julialang.org/t/convert-split-result-to-array-of-strings/8508/1 "2018-01-21T19:32:54Z")

</div>

must be obvious, but I have tried many

```julia
julia> d = Dict("One" => 1, "Two" => 2, "Three" => 3); String(keys(d))
ERROR: MethodError: Cannot `convert` an object of type Base.KeyIterator{Dict{String,Int64}} 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.

```

I have tried many variations, using convert, etc.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [January 21, 2018, 7:43pm UTC](https://discourse.julialang.org/t/convert-split-result-to-array-of-strings/8508/2 "2018-01-21T19:43:46Z")

</div>

Maybe you want `collect(keys(d))`?

---

<div class="post-metadata">

**Author:** ![ivo\_welch](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@ivo\_welch](https://discourse.julialang.org/u/ivo_welch)\
**Post date:** [January 21, 2018, 7:57pm UTC](https://discourse.julialang.org/t/convert-split-result-to-array-of-strings/8508/3 "2018-01-21T19:57:59Z")

</div>

thanks, steward, but not enough. I would like to understand how to convert this from substrings to strings. the following is an example, not to get a solution, but to illustrate the difference. I want to just get a simple one-dimensional string array out of split.

```julia
julia> mystring="a ab ac ab ab ad ba"
"a ab ac ab ab ad ba"

julia> wordcounts= Dict{String,Int64}; \
       for word in collect(split(mystring)); wordcounts[word]= get(wordcounts, word, 0) + 1; end
ERROR: MethodError: no method matching get(::Type{Dict{String,Int64}}, ::SubString{String}, ::Int64)
Closest candidates are:
  get(::ObjectIdDict, ::ANY, ::ANY) at associative.jl:434
  get(::Base.EnvHash, ::AbstractString, ::Any) at env.jl:79
  get(::Dict{K,V}, ::Any, ::Any) where {K, V} at dict.jl:478
  ...
Stacktrace:
 [1] macro expansion at ./REPL[10]:1 [inlined]
 [2] anonymous at ./<missing>:?

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [January 21, 2018, 8:30pm UTC](https://discourse.julialang.org/t/convert-split-result-to-array-of-strings/8508/4 "2018-01-21T20:30:34Z")

</div>

It works, just do `wordcounts = Dict{String,Int}()` to actually create a dict object rather than referring to the type itself. You don’t even need to call `collect`.

In general it’s a good idea to provide a reproducible example, as it makes it clear what you want to do, and what might be the problem.

---

<div class="post-metadata">

**Author:** ![ivo\_welch](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@ivo\_welch](https://discourse.julialang.org/u/ivo_welch)\
**Post date:** [January 22, 2018, 2:12am UTC](https://discourse.julialang.org/t/convert-split-result-to-array-of-strings/8508/5 "2018-01-22T02:12:09Z")

</div>

thanks. this was very helpful for my specific example, but let’s say I am not only stupid but also stubborn, and I really want to know how to convert the type of split(mystring) from an `Array{Substring{String},1})` into an `Array(String,1)`. How would I do this?

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [January 22, 2018, 3:07am UTC](https://discourse.julialang.org/t/convert-split-result-to-array-of-strings/8508/6 "2018-01-22T03:07:35Z")

</div>

`String.(split(mystring))`

---

<div class="post-metadata">

**Author:** ![ivo\_welch](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@ivo\_welch](https://discourse.julialang.org/u/ivo_welch)\
**Post date:** [January 23, 2018, 2:57am UTC](https://discourse.julialang.org/t/convert-split-result-to-array-of-strings/8508/7 "2018-01-23T02:57:08Z")

</div>

thx. perfect!
