# How to declare a string array for the purpose of concatenation

**URL:** <https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 23, 2021, 7:02pm UTC](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868 "2021-08-23T19:02:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)\
**Post date:** [August 23, 2021, 7:02pm UTC](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868/1 "2021-08-23T19:02:37Z")

</div>

Hi all,  
I have the following code of converting a string array into a char array (MATLAB’s parlance)

This works

```julia
str1 = ["abc", "def", "ghi"]

function str_join(str2)
str=str2[1]

for i=2:1:length(str2)
    
    str = string(str,str2[i]) 
    
end

    return str

end 

str3 = str_join(str1)
display(str3)

```

Output: “abcdefghi”

This doesn’t work

```julia
str1 = ["abc", "def", "ghi"]

function str_join(str2)
str = Array{String}(undef,1)

str[1]=str2[1]

for i=2:1:length(str2)
    
    str = string(str,str2[i]) 
    
end

    return str

end 

str3 = str_join(str1)
display(str3)

```

Output: “["abc"]defghi”

Is there any way to get rid of this residue of undef?

---

<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:** [August 23, 2021, 7:25pm UTC](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868/2 "2021-08-23T19:25:36Z")

</div>

The `undef` isn’t really relevant here. The point is that you’re doing `string(str, ...)`, where `str` is an array of strings (initially with one element).

`string(["abc"])` gives `["abc"]`, which just isn’t what you want here.

If you want the easy answer, the way to join strings is…`join()` 🙂

```julia
julia> str1 = ["abc", "def", "ghi"]
3-element Vector{String}:
 "abc"
 "def"
 "ghi"

julia> join(str1)
"abcdefghi"

```

---

<div class="post-metadata">

**Author:** ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)\
**Post date:** [August 23, 2021, 8:28pm UTC](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868/3 "2021-08-23T20:28:40Z")

</div>

Thank you rdeits for your input. I was used to this MATLAB’s way

```julia
str1 = ["abc", "def", "ghi"];
str2 = [];

for i=1:1:length(str1)
    
    str2 = [str2 str1(i)] ;
    
end

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 23, 2021, 8:43pm UTC](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868/4 "2021-08-23T20:43:15Z")

</div>

[https://docs.julialang.org/en/v1/manual/strings/#Common-Operations](https://docs.julialang.org/en/v1/manual/strings/#Common-Operations)

might want to give the short and sweet Manual a read.

---

<div class="post-metadata">

**Author:** ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)\
**Post date:** [August 23, 2021, 8:50pm UTC](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868/5 "2021-08-23T20:50:26Z")

</div>

Thank you jling for the link. Now going through it more carefully!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 23, 2021, 11:13pm UTC](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868/6 "2021-08-23T23:13:46Z")

</div>

FWIW, `prod(str1)` produces here the same result as `join(str1)`

---

<div class="post-metadata">

**Author:** ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)\
**Post date:** [August 24, 2021, 2:46am UTC](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868/7 "2021-08-24T02:46:48Z")

</div>

Thank you Rafael.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [August 24, 2021, 3:54am UTC](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868/8 "2021-08-24T03:54:27Z")

</div>

Adding to this, `join` lets you specify a delimiter:

```julia
julia> join(("a","b","c"), "; ")
"a; b; c" 

```

---

<div class="post-metadata">

**Author:** ![soldin](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@soldin](https://discourse.julialang.org/u/soldin)\
**Post date:** [August 24, 2021, 4:01am UTC](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868/9 "2021-08-24T04:01:53Z")

</div>

Interesting. Thank you Gustaphe.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [August 24, 2021, 7:10am UTC](https://discourse.julialang.org/t/how-to-declare-a-string-array-for-the-purpose-of-concatenation/66868/10 "2021-08-24T07:10:44Z")

</div>

Another cool use of `join` is you don’t need to create the string if you’re printing it to a file or other io:

```julia
julia> join(stdout, ("a", "b", "c"), "; ")
a; b; c
julia> open("file.txt", "w") do f
           join(f, ("a", "b", "c"), "; ")
       end

```
