Properties of Strings

Much of what the Wolfram Language does revolves around manipulating structured expressions. But you can also use the Wolfram Language as a system for handling unstructured strings of text.

Text strings.

When you input a string of text to the Wolfram Language you must always enclose it in quotes. However, when the Wolfram Language outputs the string it usually does not explicitly show the quotes.

You can see the quotes by asking for the input form of the string. In addition, in a Wolfram System notebook, quotes will typically appear automatically as soon as you start to edit a string.

When the Wolfram Language outputs a string, it usually does not explicitly show the quotes:

"This is a string."

You can see the quotes, however, by asking for the input form of the string:

InputForm[%]

The fact that the Wolfram Language does not usually show explicit quotes around strings makes it possible for you to use strings to specify quite directly the textual output you want.

The strings are printed out here without explicit quotes:

Print["The value is ",567,"."]

You should understand, however, that even though the string "x" often appears as x in output, it is still a quite different object from the symbol x.

The string "x" is not the same as the symbol x:

"x"===x

You can test whether any particular expression is a string by looking at its head. The head of any string is always (RefLink[String, paclet:ref/String]).

All strings have head (RefLink[String, paclet:ref/String]):

Head["x"]

The pattern _String matches any string:

Cases[{"ab",x,"a",y},_String]

You can use strings just like other expressions as elements of patterns and transformations. Note, however, that you cannot assign values directly to strings.

This gives a definition for an expression that involves a string:

z["gold"]=79

This replaces each occurrence of the string "aa" by the symbol x:

{"aaa","aa","bb","aa"}/."aa"->x