2.9.13 Converting between Strings, Boxes and Expressions
| ToString[expr, form] | create a string representing the specified textual form of expr | | ToBoxes[expr, form] | create boxes representing the specified textual form of expr | | ToExpression[input, form] | create an expression by interpreting a string or boxes as input in the specified textual form | | ToString[expr] | create a string using OutputForm | | ToBoxes[expr] | create boxes using StandardForm | | ToExpression[input] | create an expression using StandardForm |
Converting between strings, boxes and expressions. | Here is a simple expression. | |
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. | |
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. | |
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=
|
|
| InputForm | strings corresponding to keyboard input | | StandardForm | strings or boxes corresponding to standard two-dimensional input (default) | | TraditionalForm | strings or boxes mimicking traditional mathematical notation |
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]=
|
|
| ToExpression[input, form, h] | create an expression, then wrap it with head h |
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. | |
Out[18]=
|
|
| SyntaxQ["string"] | determine whether a string represents syntactically correct Mathematica input | | SyntaxLength["string"] | find out how long a sequence of characters starting at the beginning of a string is syntactically correct |
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"]
|
Out[19]=
|
|
| ToExpression requires that the string correspond to a complete Mathematica expression. | |
In[20]:=
ToExpression["1 + 2 + "]
|
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]=
|
|
|