Special Characters in Strings

In addition to the ordinary characters that appear on a standard keyboard, you can include in Wolfram Language strings any of the special characters that are supported by the Wolfram Language.

Here is a string containing special characters:

"α⊕β⊕…"

You can manipulate this string just as you would any other:

StringReplace[%,"⊕"->" ⊙⊙ "]

Here is the list of the characters in the string:

Characters[%]

In a Wolfram System notebook, a special character such as α can always be displayed directly. But if you use a text-based interface, then often the only characters that can readily be displayed are the ones that appear on your keyboard. Exactly which special characters can be displayed is inferred from the value of (RefLink[$CharacterEncoding, paclet:ref/$CharacterEncoding]).

As a result, what the Wolfram System does in such situations is to try to approximate special characters by similar-looking sequences of ordinary characters. And when this is not practical, the Wolfram System just gives the full name of the special character.

In a Wolfram System notebook using (RefLink[StandardForm, paclet:ref/StandardForm]), special characters can be displayed directly:

"Lamé ⟶ αβ+"

In (RefLink[OutputForm, paclet:ref/OutputForm]), however, special characters that cannot be displayed exactly are approximated when possible by sequences of ordinary ones:

ToString["Lamé ⟶ αβ+",OutputForm,CharacterEncoding->"WindowsANSI"]

When using (RefLink[InputForm, paclet:ref/InputForm]) or (RefLink[FullForm, paclet:ref/FullForm]), special characters are not approximated. The Wolfram Language uses full names for non-representable special characters in (RefLink[InputForm, paclet:ref/InputForm]), while (RefLink[FullForm, paclet:ref/FullForm]) always uses long names, even in the notebook interface.

In (RefLink[InputForm, paclet:ref/InputForm]), all characters not part of the encoding—in this case the special characters other than é—are written using long names:

ToString["Lamé ⟶ αβ+",InputForm,CharacterEncoding->"WindowsANSI"]

In (RefLink[FullForm, paclet:ref/FullForm]), all special characters are written using long names:

"Lamé ⟶ αβ+"//FullForm

By default, the Wolfram System uses the character encoding "PrintableASCII" when saving notebooks and packages. This means that when special characters are written out to files or external programs, they are represented purely as sequences of ordinary characters. This uniform representation is crucial in allowing special characters in the Wolfram Language to be used in a way that does not depend on the details of particular computer systems.

When creating packages and notebooks, special characters are always written out using full names:

ExportString["Lamé ⟶ αβ+","Package"]

Use the "PrintableASCII" to create strings with no special characters:

ToString["Lamé ⟶ αβ+",OutputForm,CharacterEncoding->"PrintableASCII"]

In (RefLink[InputForm, paclet:ref/InputForm]), all special characters are written out fully when using "PrintableASCII":

ToString["Lamé ⟶ αβ+",InputForm,CharacterEncoding->"PrintableASCII"]

Ways to enter characters in a string.

You have to use \ to "escape" any " or \ characters in strings that you enter:

"Strings can contain \"quotes\" and \\ characters."

\\ produces a literal \ rather than forming part of the specification of α:

"\\[Alpha] is α."

This breaks the string into a list of individual characters:

Characters[%]

This creates a list of the characters in the full name of α:

Characters[ToString[FullForm["α"]]]

And this produces a string consisting of an actual α from its full name:

ToExpression["\"\\["<>"Alpha"<>"]\""]