Producing Strings with split()

Hi,

I would like to avoid getting SubStrings.
While the first split can be “fixed”, it “fails” at the second split and I don’t get why.

julia> txt = "a b c-aa bb cc-aaa bbb ccc"
"a b c-aa bb cc-aaa bbb ccc"

julia> lines = split(txt, "-")
3-element Vector{SubString{String}}:
 "a b c"
 "aa bb cc"
 "aaa bbb ccc"

julia> str_lines = string.(lines)
3-element Vector{String}:
 "a b c"
 "aa bb cc"
 "aaa bbb ccc"

julia> cols = split.(str_lines)
3-element Vector{Vector{SubString{String}}}:
 ["a", "b", "c"]
 ["aa", "bb", "cc"]
 ["aaa", "bbb", "ccc"]

julia> str_cols = string.(cols)
3-element Vector{String}:
 "SubString{String}[\"a\", \"b\", \"c\"]"
 "SubString{String}[\"aa\", \"bb\", \"cc\"]"
 "SubString{String}[\"aaa\", \"bbb\", \"ccc\"]"

What your last input gives you is the result of string applied to each element of cols. I assume what you want is the result of string applied to each element of each element of cols, which you can get as follows:

str_cols = map(col -> string.(col), cols)

An alternative approach is

split_to_strings(s, args...) = String.(split(s, args...))
lines = split_to_strings(txt, "-")
cols = split_to_strings.(lines)
1 Like

Thanks !

An alternative:

broadcast.(String, cols)
2 Likes