I have reviewed many string replacement Q&A but I have been unable to apply them to this case (MWE):
three = 3^"1";
FullForm[three]
2 three
three /. String -> Integer;
2 three
Replace[three, n : NumberString :> ToExpression[n], All];
FullForm[2 three]
(* Power[3,"1"]
2 3^(1)
2 3^(1)
Times[2,Power[3,"1"]] *)
So we see that each replacement actually leaves the String untouched.
The documentation on ReplaceAll etc. is however explicit that one can replace heads; String is a Head; why can it not be replace like Sin?
In[1]:= Sin[x]/.Sin->Cos
Out[1]= Cos[x]
Why can I not convert a string to an integer in this way, and how would I convert "three" above? (the expression I am interested in fixing up is of course a bit longer than this)
EDIT
The actual expression of interest is the output of
FullForm[thwPoly[m17]]
which is
Times[Plus[Times[128,Power[3,"0"]],Times[64,Power[3,"1"]],Times[32,Power[3,"2"]],Times[8,Power[3,"3"]],Times[4,Power[3,"4"]],Times[2,Power[3,"5"]],Power[3,"6"]],Power[Plus[Power[2,"11"],Times[-1,Power[3,"7"]]],-1]]
The next attempt:
ToExpression/@thwPoly[m17]
gives: ToExpression::notstrbox: 128 3^(0)+64 3^(1)+32 3^(2)+8 3^(3)+4 3^(4)+2 3^(5)+3^(6) is not a string or a box. ToExpression can only interpret strings or boxes as Wolfram Language input.
Attending to the need to consider levels of application of Map, I then also tried
Map[ToExpression, thwPoly[m17], Infinity]
which gave: ToExpression::esntx: Could not parse -(1/139) as input, which is interesting as that is a calculated intermediate result/.
Success Using
Replace[thwPoly[m17], n_String :> FromDigits[n], All]
gives -17, the correct value.
So, the conversion has been achieved - but I'm still not clear why head replacement doesn't work.
three = 3^"1"
does not have headString
. $\endgroup$