Is there a sprintf()
command (some command that takes a printf
-style format string and a list of values to insert into the string) or something very much like it (preferably with a similar style of format specifiers)? Or, alternately, how would I implement sprintf()
in Mathematica?
Answer
I've had a need for such a function several times, and I found this implementation of C-style *printf
functions, by Vlad Seghete. To use it, all you need to do is extract the files to $UserBaseDirectory/MathPrintF/
and you're all set.
Here's an example once you've installed it:
< sprintf["%d %s %d %s, %s %s %s %s",
Sequence @@ Riffle[{1, 2, "red", "blue"}, {"fish"}, {2, -1, 2}]]
Out[1]= 1 fish 2 fish, red fish blue fish
Also note the following caveat in the README
Limited Functionality
While we tried to mimic the C-standard as much as possible, only certain features are implemented. These are mainly dictated by what we needed at the time. In particular %d, %f, %e, %E and %s with most of their options are implemented.
Comments
Post a Comment