ToString


ToString has several options that I won't address here because I never became
familiar with them. However, there is an interesting problem with converting
a symbol to a string.  How can we convert a symbol to a string without
letting the symbol evaluate?  ToString2 below does the job. Notice evaluation
has to be prevented in two places here.

mass = 56 ;    ClearAll[ToString2]    Attributes[ToString2] = {HoldAll} ;    ToString2[a_] := ToString[Unevaluated[a]]


In the next cell we get the characters used to make the Symbol mass without
letting the symbol evaluate.

InputForm[ToString2[mass]]

mass


Sometimes you might want a function to convert one of it's arguments to a
string before the argument evaluates. Then you might want your function to do
something with the resulting string. In this case a pure function can be used
to do the string conversion. The next cell defines a function Letters that
returns the characters used to make the argument before the argument has a
chance to evaluate.

ClearAll[Letters] ;  Attributes[Letters] = {HoldAll} ;  Letters[s_] := Characters[Function[a,  ToString[Unevaluated[a]],  {HoldAll}] [s]    ]

Letters[mass]

{m, a, s, s}

Notice mass still has the value that was assigned above.

mass

56


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page