Concatenate Strings to a Vector of Strings

Viewed 167

I have a string vector

faces = ["bold", "ital", "code"]

3-element Vector{String}:
 "bold"
 "ital"
 "code"

and I have a scalar String

"The font face is "

that I want to combine to a Vector{String}

["The font face is bold", "The font face is ital", "The font face is code"]

I could swear I did this before using $faces, but I can't remember how.


Additional clarification:
I am looking for a way to insert the elements of the vector into the string. So, while "The font face is " .* faces works for this example I am looking (also) for the solution that puts the vector values inside the string. A solution that would work like

String.("The font face is $(faces)")
3 Answers

From the last line of your question, may be you had a patttern like this in mind:

julia> string.("The font face is ", faces)
3-element Vector{String}:
 "The font face is bold"
 "The font face is ital"
 "The font face is code"

Semantically, this and the .* solution and the map solution are ultimately doing the same thing, but perhaps you have a preference for this syntax.

faces = ["bold", "ital", "code"]
str = "The font face is "

map(x -> str*x, faces)

List comprehension is an option as well:

["The font face is $f" for f ∈ faces]

This answer is from Nils Gudat in the comments above. (Happy to remove it if he wants to post it.)

Related