This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
Wolfram Research, Inc.

String Representation of BoxesThe Syntax of the Mathematica Language

2.8.13 Converting between Strings, Boxes and Expressions

Converting between strings, boxes and expressions.

Here is a simple expression.

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

Out[1]=

This gives the InputForm of the expression as a string.

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

Out[2]=

In FullForm explicit quotes are shown around the string.

In[3]:= FullForm[%]

Out[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

Out[4]//FullForm=

ToBoxes yields the boxes themselves.

In[5]:= ToBoxes[x^2 + y^2, StandardForm]

Out[5]=

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]

Out[6]=

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

In[7]:= FullForm[%]

Out[7]//FullForm=

The string looks right only in a monospaced font.

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

Out[8]//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"]

Out[10]=

This creates the same expression from StandardForm boxes.

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

SuperscriptBox["y", "2"]}]]

Out[11]=

Here the boxes are represented in InputForm.

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

Out[12]=

This returns raw boxes.

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

Out[13]=

This interprets the boxes.

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

Out[14]=

In TraditionalForm these are interpreted as functions.

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

Out[15]=

Creating expressions wrapped with special heads.

This creates an expression, then immediately evaluates it.

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

Out[16]=

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

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

Out[17]=

You can get rid of the Hold using ReleaseHold.

In[18]:= ReleaseHold[%]

Out[18]=

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". ^

Out[19]=

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

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

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

Out[20]=

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"]

Out[21]=

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

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

Out[22]=

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 + "]

Out[23]=

String Representation of BoxesThe Syntax of the Mathematica Language