me , colleague have different versions of visualstudio. used interpolated string , couldn't build solution , had convert them string.format.
now thought might exercise regex.
so how convert this:
$"alpha: {alphaid}, betavalue: {beta.value}"
to this:
string.format("alpha: {0}, betavalue: {1}", alphaid, beta.value)
now number of variables can vary (let's 1 - 20, should generic)
i came regex, match first variable
\$.*?{(\w+)}
but couldn't figure out how repeat part after dollar sign, can repeat result.
regex.replace
has overload takes function, called matchevaluator
. might use this;
var paramnumber = 0; var idnames = new list<string>(); mycsharpstring = regex.replace(mycsharpstring, match => { // remember id inside brackets; idnames.add(match.tostring()); // return "0", "1", etc. return (paramnumber++).tostring(); });
at end of process, string "this {foo} not {bar}"
have been replaced "this {0} not {1}"
, have list containing { "foo" , "bar" }
can use assemble parameter list.
Comments
Post a Comment