# A Question about Vectors with strings

**URL:** <https://discourse.julialang.org/t/a-question-about-vectors-with-strings/68661>\
**Category:** New to Julia\
**Tags:** strings, vector\
**Created:** [September 23, 2021, 11:23pm UTC](https://discourse.julialang.org/t/a-question-about-vectors-with-strings/68661 "2021-09-23T23:23:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesus-117](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jesus-117/32/29446_2.png) [@Jesus-117](https://discourse.julialang.org/u/Jesus-117)\
**Post date:** [September 23, 2021, 11:23pm UTC](https://discourse.julialang.org/t/a-question-about-vectors-with-strings/68661/1 "2021-09-23T23:23:09Z")

</div>

I’m new to julia, out of curiosity, I thought about recreating a problem that I did in Matlab at the time: With compact notation write a vector that has as odd entries the numbers from 1 to 100 in string form and the odd entries that are “+” (that is, something of the form [“1”, “+”, “2” , …, “+”, “100”]). Using that array and string concatenation, print the sum and the evaluation of that sum (you don’t evaluate it, it is evaluated as part of the final string. That is, the final result should print something of the type "1 + 2 + 3 + 4 + 5 = 15 "

And my problem is just How i wrote this vector in Julia [“1”, “+”, “2” , …, “+”, “100”]?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 23, 2021, 11:44pm UTC](https://discourse.julialang.org/t/a-question-about-vectors-with-strings/68661/2 "2021-09-23T23:44:28Z")

</div>

Here’s one way:

```julia
julia> x = Vector{String}(undef, 199);

julia> counter = 1
1

julia> for i in eachindex(x)
           if isodd(counter)
               x[i] = string(Int((counter + 1) / 2))
           else
               x[i] = "+"
           end
           counter = counter + 1
       end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 23, 2021, 11:49pm UTC](https://discourse.julialang.org/t/a-question-about-vectors-with-strings/68661/3 "2021-09-23T23:49:24Z")

</div>

That can be a comprehension as well:

```julia
[isodd(i) ? "$(div(i+1,2))" : "+" for i in 1:199]

```

---

<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:** [September 24, 2021, 3:25am UTC](https://discourse.julialang.org/t/a-question-about-vectors-with-strings/68661/4 "2021-09-24T03:25:57Z")

</div>

This is even more compact, and (I think) more clear:

```julia
split(join(string.(1:100), '+'), r"\b")

```

> [@Jesus-117](#):
>
> Using that array and string concatenation, print the sum and the evaluation of that sum

```julia
println(join(string.(1:100), '+'), " = ", sum(1:100))

```

---

<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:** [September 24, 2021, 7:56am UTC](https://discourse.julialang.org/t/a-question-about-vectors-with-strings/68661/5 "2021-09-24T07:56:58Z")

</div>

@stevengj, a variant without recurring to cryptic regex:  
`split(join(string.(1:100), ".+."), '.')`

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [September 24, 2021, 1:34pm UTC](https://discourse.julialang.org/t/a-question-about-vectors-with-strings/68661/6 "2021-09-24T13:34:43Z")

</div>

Or just

```julia
split(join(string.(1:100), " + "))

```
