将字符串连接到字符串向量
Concatenate Strings to a Vector of Strings
我有一个字符串向量
faces = ["bold", "ital", "code"]
3-element Vector{String}:
"bold"
"ital"
"code"
我有一个标量 String
"The font face is "
我想合并成 Vector{String}
["The font face is bold", "The font face is ital", "The font face is code"]
我可以发誓我在使用 $faces
之前就这样做了,但我不记得是怎么做到的。
补充说明:
我正在寻找一种将向量的元素插入到字符串中的方法。因此,虽然 "The font face is " .* faces
适用于此示例,但我正在(也)寻找将向量值放入字符串中的解决方案。一个可以像
一样工作的解决方案
String.("The font face is $(faces)")
faces = ["bold", "ital", "code"]
str = "The font face is "
map(x -> str*x, faces)
根据你问题的最后一行,你可能有这样的模式:
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"
从语义上讲,这个和 .*
解决方案以及地图解决方案最终做的是同一件事,但也许您更喜欢这种语法。
列表理解也是一个选项:
["The font face is $f" for f ∈ faces]
这个答案来自上面评论中的 Nils Gudat。 (如果他想要 post,很乐意将其删除。)
我有一个字符串向量
faces = ["bold", "ital", "code"]
3-element Vector{String}:
"bold"
"ital"
"code"
我有一个标量 String
"The font face is "
我想合并成 Vector{String}
["The font face is bold", "The font face is ital", "The font face is code"]
我可以发誓我在使用 $faces
之前就这样做了,但我不记得是怎么做到的。
补充说明:
我正在寻找一种将向量的元素插入到字符串中的方法。因此,虽然 "The font face is " .* faces
适用于此示例,但我正在(也)寻找将向量值放入字符串中的解决方案。一个可以像
String.("The font face is $(faces)")
faces = ["bold", "ital", "code"]
str = "The font face is "
map(x -> str*x, faces)
根据你问题的最后一行,你可能有这样的模式:
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"
从语义上讲,这个和 .*
解决方案以及地图解决方案最终做的是同一件事,但也许您更喜欢这种语法。
列表理解也是一个选项:
["The font face is $f" for f ∈ faces]
这个答案来自上面评论中的 Nils Gudat。 (如果他想要 post,很乐意将其删除。)