This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Textual Input and Output /

2.9.13 Converting between Strings, Boxes and Expressions

Converting between strings, boxes and expressions.

Here is a simple expression.

In[1]:= x^2 + y^2


This gives the InputForm of the expression as a string.

In[2]:= ToString[x^2 + y^2, InputForm]


In FullForm explicit quotes are shown around the string.

In[3]:= FullForm[%]


This gives a string representation for the StandardForm boxes that correspond to the expression.

In[4]:= ToString[x^2 + y^2, StandardForm] // FullForm


ToBoxes yields the boxes themselves.

In[5]:= 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 OutputForm.

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

In[6]:= ToString[x^2 + y^2, OutputForm]


The string consists of two lines, separated by an explicit \n newline.

In[7]:= FullForm[%]


The string looks right only in a monospaced font.

In[8]:= StyleBox[%, FontFamily->"Times"] // DisplayForm


If you operate only with one-dimensional structures, you can effectively use ToString to do string manipulation with formatting functions.

This generates a string corresponding to the OutputForm of StringForm.

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

Out[9]//InputForm= "4^10 = 1048576"

Some forms handled by ToExpression.

This creates an expression from an InputForm string.

In[10]:= ToExpression["x^2 + y^2"]


This creates the same expression from StandardForm boxes.

In[11]:= ToExpression[RowBox[{SuperscriptBox["x", "2"], "+",
SuperscriptBox["y", "2"]}]]


Here the boxes are represented in InputForm.

In[12]:= ToExpression[\(x\^2 + y\^2\)]


This returns raw boxes.

In[13]:= ToExpression["\(x\^2 + y\^2\)"]


This interprets the boxes.

In[14]:= ToExpression["\!\(x\^2 + y\^2\)"]


In TraditionalForm these are interpreted as functions.

In[15]:= ToExpression["c(1 + x) + log(x)", TraditionalForm]


Creating expressions wrapped with special heads.

This creates an expression, then immediately evaluates it.

In[16]:= ToExpression["1 + 1"]


This creates an expression using StandardForm rules, then wraps it in Hold.

In[17]:= ToExpression["1 + 1", StandardForm, Hold]


You can get rid of the Hold using ReleaseHold.

In[18]:= ReleaseHold[%]


Testing correctness of strings as input.

ToExpression will attempt to interpret any string as Mathematica input. But if you give it a string that does not correspond to syntactically correct input, then it will print a message, and return $Failed.

This is not syntactically correct input, so ToExpression does not convert it to an expression.

In[19]:= ToExpression["1 +/+ 2"]

ToExpression::sntx: Syntax error in or before "1 +/+ 2". ^


ToExpression requires that the string correspond to a complete Mathematica expression.

In[20]:= ToExpression["1 + 2 + "]

ToExpression::sntxi: Incomplete expression; more input is needed.


You can use the function SyntaxQ to test whether a particular string corresponds to syntactically correct Mathematica input. If SyntaxQ returns False, you can find out where the error occurred using SyntaxLength. SyntaxLength returns the number of characters which were successfully processed before a syntax error was detected.

SyntaxQ shows that this string does not correspond to syntactically correct Mathematica input.

In[21]:= SyntaxQ["1 +/+ 2"]


SyntaxLength reveals that an error was detected after the third character in the string.

In[22]:= SyntaxLength["1 +/+ 2"]


Here 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.

In[23]:= SyntaxLength["1 + 2 + "]