2
$\begingroup$

The command

ToString[StringForm["SomeText=`` as well as OtherText=``.", "textA", "textB"], FormatType -> InputForm]

produces the output

StringForm["SomeText=`` as well as OtherText=``.", "textA", "textB"]

What I would like to produce instead is

"SomeText=textA as well as OtherText=textB"

The problem, of course disappears if I remove the FormatType->InputForm. However I need it, because StringForm otherwise produces a string which consistes of multiple lines if the parameters to StringForm are other objects like numerical values with exponents.

I tried various combinations with Hold and Evaluate, but could not change this behaviour to what I need.

$\endgroup$
0
$\begingroup$
fn[args__] :=
 Replace[
   StringForm[args],
   x : Except[_String] :> ToString[x, InputForm],
   1
 ] // ToString

Test:

fn["SomeText=`` as well as OtherText=``.", 2.0*10^(-15), 2/5]
"SomeText=2.*^-15 as well as OtherText=2/5."
|improve this answer|||||
$\endgroup$
3
$\begingroup$
InputForm[
 ToString@StringForm["SomeText=`` as well as OtherText=``.", "textA", "textB"]]

"SomeText=textA as well as OtherText=textB."

If you have version 10 you might want to try StringTemplate

StringTemplate["SomeText=`` as well as OtherText=``."]["textA", "textB"]

"SomeText=textA as well as OtherText=textB."

|improve this answer|||||
$\endgroup$
  • $\begingroup$ InputForm[ ToString@StringForm["SomeText=`` as well as OtherText=``.", 2.0*10^(-15), "textB"]] would however result in " -15\nSomeText=2. 10 as well as OtherText=textB." which is also not what I want. It should become "SomeText=2. 10^-15 as well as OtherText=textB." or similar. $\endgroup$ – highsciguy Apr 12 '15 at 19:44
  • $\begingroup$ @highsciguy Couldn't you use ToString[2.0*10^(-15), InputForm] for your arguments? $\endgroup$ – Sjoerd C. de Vries Apr 12 '15 at 19:54
  • $\begingroup$ Yes, I could. However, I would need to wrap every single number in my outputs with ToString then. $\endgroup$ – highsciguy Apr 12 '15 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.