Converting between Strings, Boxes, and Expressions

Converting between strings, boxes, and expressions.

Here is a simple expression:

x^2+y^2

This gives the (RefLink[InputForm, paclet:ref/InputForm]) of the expression as a string:

ToString[x^2+y^2,InputForm]

In (RefLink[FullForm, paclet:ref/FullForm]) explicit quotes are shown around the string:

FullForm[%]

This gives a string representation for the (RefLink[StandardForm, paclet:ref/StandardForm]) boxes that correspond to the expression:

ToString[x^2+y^2,StandardForm]//FullForm

(RefLink[ToBoxes, paclet:ref/ToBoxes]) yields the boxes themselves:

ToBoxes[x^2+y^2,StandardForm]

In generating data for files and external programs, it is sometimes necessary to produce two-dimensional forms which use only ordinary keyboard characters. You can do this using (RefLink[OutputForm, paclet:ref/OutputForm]).

This produces a string which gives a two-dimensional rendering of the expression, using only ordinary keyboard characters:

ToString[x^2+y^2,OutputForm]

The string consists of two lines, separated by an explicit ∖n newline:

FullForm[%]

The string looks right only in a monospaced font:

Style[%,FontFamily->"Times"]

If you operate only with one-dimensional structures, you can effectively use (RefLink[ToString, paclet:ref/ToString]) to do string manipulation with formatting functions.

This generates a string corresponding to the (RefLink[OutputForm, paclet:ref/OutputForm]) of (RefLink[StringForm, paclet:ref/StringForm]):

ToString[StringForm["``^10 = ``",4,4^10]]//InputForm

Some forms handled by (RefLink[ToExpression, paclet:ref/ToExpression]).

This creates an expression from an (RefLink[InputForm, paclet:ref/InputForm]) string:

ToExpression["x^2 + y^2"]

This creates the same expression from (RefLink[StandardForm, paclet:ref/StandardForm]) boxes:

ToExpression[RowBox[{SuperscriptBox["x","2"],"+",SuperscriptBox["y","2"]}]]

In (RefLink[TraditionalForm, paclet:ref/TraditionalForm]) these are interpreted as functions:

ToExpression["c(1 + x) + log(x)",TraditionalForm]

Creating expressions wrapped with special heads.

This creates an expression, then immediately evaluates it:

ToExpression["1 + 1"]

This creates an expression using (RefLink[StandardForm, paclet:ref/StandardForm]) rules, then wraps it in (RefLink[Hold, paclet:ref/Hold]):

ToExpression["1 + 1",StandardForm,Hold]

You can get rid of the (RefLink[Hold, paclet:ref/Hold]) using (RefLink[ReleaseHold, paclet:ref/ReleaseHold]):

ReleaseHold[%]

Testing correctness of strings as input.

(RefLink[ToExpression, paclet:ref/ToExpression]) will attempt to interpret any string as Wolfram Language input. But if you give it a string that does not correspond to syntactically correct input, then it will print a message, and return (RefLink[$Failed, paclet:ref/$Failed]).

This is not syntactically correct input, so (RefLink[ToExpression, paclet:ref/ToExpression]) does not convert it to an expression:

ToExpression["1 +/+ 2"]

(RefLink[ToExpression, paclet:ref/ToExpression]) requires that the string correspond to a complete Wolfram Language expression:

ToExpression["1 + 2 + "]

You can use the function (RefLink[SyntaxQ, paclet:ref/SyntaxQ]) to test whether a particular string corresponds to syntactically correct Wolfram Language input. If (RefLink[SyntaxQ, paclet:ref/SyntaxQ]) returns (RefLink[False, paclet:ref/False]), you can find out where the error occurred using (RefLink[SyntaxLength, paclet:ref/SyntaxLength]). (RefLink[SyntaxLength, paclet:ref/SyntaxLength]) returns the number of characters which were successfully processed before a syntax error was detected.

(RefLink[SyntaxQ, paclet:ref/SyntaxQ]) shows that this string does not correspond to syntactically correct Wolfram Language input:

SyntaxQ["1 +/+ 2"]

(RefLink[SyntaxLength, paclet:ref/SyntaxLength]) reveals that an error was detected after the third character in the string:

SyntaxLength["1 +/+ 2"]

Here (RefLink[SyntaxLength, paclet:ref/SyntaxLength]) returns a value greater than the length of the string, indicating that the input was correct so far as it went, but needs to be continued:

SyntaxLength["1 + 2 + "]