# Parse vector from string

**URL:** <https://discourse.julialang.org/t/parse-vector-from-string/10728>\
**Category:** General Usage\
**Tags:** strings, sparse\
**Created:** [May 5, 2018, 4:14pm UTC](https://discourse.julialang.org/t/parse-vector-from-string/10728 "2018-05-05T16:14:02Z")\
**Posts on this page:** 5\
**Page:** 2

<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:** [November 6, 2022, 3:37pm UTC](https://discourse.julialang.org/t/parse-vector-from-string/10728/21 "2022-11-06T15:37:47Z")

</div>

A simple proposal:

```julia
str = "[1, 2, 3]" 
parse.(Int, split(chop(str, head=1),','))

```

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [March 27, 2023, 8:34pm UTC](https://discourse.julialang.org/t/parse-vector-from-string/10728/22 "2023-03-27T20:34:40Z")

</div>

I ran into the same need as OP and @rafael.guerra’s suggestion worked perfectly.

```julia
julia> abc[1]
"[2.507612778, 2.507612778, 2.507612778, 0.563968079, 0.488410684, 0.488410684]"

julia> parse.(Float64,split(chop(abc[1],head=1),","))
6-element Vector{Float64}:
 2.507612778
 2.507612778
 2.507612778
 0.563968079
 0.488410684
 0.488410684

```

---

<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:** [March 27, 2023, 8:48pm UTC](https://discourse.julialang.org/t/parse-vector-from-string/10728/23 "2023-03-27T20:48:17Z")

</div>

```julia
using DelimitedFiles
@views vec(readdlm(IOBuffer(str[2:end-1]), ','))

```

may do less allocations? The CSV package is probably(?) even faster, but I find it a bit non-intuitive to use if you don’t want a `DataFrame`:

```julia
using CSV
@views [row[1] for row in CSV.File(IOBuffer(str[2:end-1]), header=0, transpose=true)]

```

_Update:_ `@btime` says that `parse.(Float64, split(chop(str, head=1),','))`, or ≈equivalently ` @views parse.(Float64, split(str[2:end-1],','))`, is actually the fastest for `str = "[2.507612778, 2.507612778, 2.507612778, 0.563968079, 0.488410684, 0.488410684]"`. CSV.jl is really optimized only for large files, I’m guessing — both it and `DelimitedFiles` currently allocate like crazy for parsing tiny strings like this.

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [March 27, 2023, 9:18pm UTC](https://discourse.julialang.org/t/parse-vector-from-string/10728/24 "2023-03-27T21:18:49Z")

</div>

Thanks for the update. And I see that (forehead slap) `str[2:end-1]` is a bit more direct than `chop(str, head=1)`. I guess I could have figured that out with a bit of thought…

---

<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:** [March 27, 2023, 9:24pm UTC](https://discourse.julialang.org/t/parse-vector-from-string/10728/25 "2023-03-27T21:24:00Z")

</div>

> [@Gus\_Hart](#):
>
> And I see that (forehead slap) `str[2:end-1]` is a bit more direct than `chop(str, head=1)`.

Note that `chop` returns a `SubString` whereas `str[2:end-1]` returns a copy, unless you use `@views` (or similar) for the latter.

(`str[2:end-1]` also only works if the characters are ASCII, whereas `chop` can also handle arbitrary Unicode characters. For example, `"[α]"[2:end-1]` fails because `end-1` is not a valid index.)

[Previous page](https://discourse.julialang.org/t/parse-vector-from-string/10728.md?page=1)
