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.
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.
If you operate only with one‐dimensional structures, you can effectively use ToString to do string manipulation with formatting functions.
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.
ToExpression[input,form,h] | create an expression, then wrap it with head h |
Creating expressions wrapped with special heads.
SyntaxQ["string"] | determine whether a string represents syntactically correct Wolfram Language 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 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 $Failed.
You can use the function SyntaxQ to test whether a particular string corresponds to syntactically correct Wolfram Language 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.