You could do this using Mata too.
local try "a b c"
mata: st_local("try2", invtokens("d" :+ tokens(st_local("try"))))
assert "`try2'" == "da db dc"
In words, this is what the second line does, explaining the innermost function first:
st_local("try")
: Access the contents in the local variable. This should evaluate to "a b c".
tokens("a b c")
: Split the string into tokens, e.g. tokens("a b c")
-> ("a", "b", "c")
.
"d" :+ ("a", "b", "c")
: In Mata, you can concatenate strings with a +
, and here :+
does this element-wise, so the result would be ("da", "db", dc").
invtokens(("da", "db", dc"))
: Put the tokens back into a string, i.e. invtokens(("da", "db", dc"))
-> "da db dc"
.
st_local("try2", "da db dc")
: The Mata equivalent of local try2 "da db dc"
.
You can find out more about the Mata functions st_local()
, tokens()
, and invtokens()
with e.g. help mf_st_local
.