Converting between Strings, Boxes, and Expressions
Converting between strings, boxes, and expressions.
Here is a simple expression.
This gives the InputForm
of the expression as a string.
explicit quotes are shown around the string.
This gives a string representation for the StandardForm
boxes that correspond to the expression.
yields the boxes themselves.
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.
The string consists of two lines, separated by an explicit
The string looks right only in a monospaced font.
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.
This creates an expression from an InputForm
|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.
|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.
requires that the string correspond to a complete Mathematica
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.
shows that this string does not correspond to syntactically correct Mathematica
reveals that an error was detected after the third character in the string.
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.