({{, (RefLink[String, paclet:ref/String])
is the head of a character string "text". }})

OpenersNotesSection(Spacer1)Details

Strings can contain any sequence of ordinary or special characters. »

_String can be used as a pattern that represents a string.

In (RefLink[FullForm, paclet:ref/FullForm]), special characters in strings are given as \[Name] or \:code. »

If an explicit newline in the string is preceded by a single backslash \, the backslash and newline are ignored.

OpenersPrimaryExamplesSection(Spacer1)Examples  (24)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Enter a string:

"This is a string."

(RefLink[InputForm, paclet:ref/InputForm]) shows quotes:

InputForm[%]

OpenersExampleSection1(Spacer1)Scope  (6)

Enter explicit newlines using \n:

"one\ntwo\nthree"

"" is the empty string, which normally does not print:

{"",x,"",""}

(RefLink[InputForm, paclet:ref/InputForm]) explicitly shows the empty strings:

InputForm[%]

Strings can contain any sequence of ordinary and special characters:

"◆ String αβγ ⟷ 𝒜ℬ𝒞 ↵"

In (RefLink[FullForm, paclet:ref/FullForm]), full character names are used:

FullForm[%]

Strings preserve internal formatting:

"red blue green italic"

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

Head["a string"]

_String matches any string, but not a symbol:

Cases[{1,2,"ab","cd",x,y},_String]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (2)

Strings can have any expression embedded:

"ab ∫(1/x)\[DifferentialD]x cd"

FullForm[%]

Strings can contain graphics:

"ab  cd"

FullForm[%]

OpenersExampleSection3(Spacer1)Applications  (5)

Print text (the quotes do not appear by default):

Print["The answer is ",2^45]

Create formatted text:

Style["This is a string.",FontSize->25,FontFamily->"Helvetica"]

Define a function that applies only to strings:

f[s_String]:=StringJoin[RotateLeft[Characters[s]]]

{f["X"],f["ABCDEF"],f[x],f[{a,b,c}]}

Use strings as plot markers:

ListPlot[{{1,2,3},{3,1,4}},PlotMarkers->{"A","B"}]

Give text in graphics:

Graphics[{Text["abcde",{0,-2}],Disk[{0,0}],Text["ABCDE",{0,2}]}]

OpenersExampleSection4(Spacer1)Properties & Relations  (5)

Break a string into characters:

Characters["This is a string."]

Reconstruct the string:

StringJoin[%]

Find character codes for characters in a string:

ToCharacterCode["This is a string."]

Reconstruct the string:

FromCharacterCode[%]

Turn a string into an expression:

ToExpression["1+x^4"]

(RefLink[ToString, paclet:ref/ToString]) creates strings, by default with (RefLink[OutputForm, paclet:ref/OutputForm]):

ToString[1+x^4]

FullForm[%]

Create a string containing (RefLink[InputForm, paclet:ref/InputForm]):

ToString[1+x^4,InputForm]

FullForm[%]

OpenersExampleSection5(Spacer1)Possible Issues  (5)

Use \" to enter a " inside a string:

"a string with \"quotes\""

Strings are treated as "atoms" with no parts:

Length["a string"]

Use (RefLink[StringLength, paclet:ref/StringLength]) to find the character length of a string:

StringLength["a string"]

Strings do not automatically insert spacing around operator characters:

"x->a+b+c+d"

x->a+b+c+d

An (RefLink[InputForm, paclet:ref/InputForm]) string contains explicit space characters:

ToString[%,InputForm]

The actual sequences of characters in a formatted string can be fairly complicated:

FullForm["red italic"]

FullForm["ab  cd"]

A string with special characters usually does not include the long form of the character:

"α"

Force long form names to be used:

ToString[FullForm["α"]]

ToString["α",InputForm,CharacterEncoding->None]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ToExpression, paclet:ref/ToExpression])  ▪  (RefLink[ToString, paclet:ref/ToString])  ▪  (RefLink[Characters, paclet:ref/Characters])  ▪  (RefLink[StringExpression, paclet:ref/StringExpression])  ▪  (RefLink[RegularExpression, paclet:ref/RegularExpression])  ▪  (RefLink[StringQ, paclet:ref/StringQ])  ▪  (RefLink[SyntaxQ, paclet:ref/SyntaxQ])  ▪  (RefLink[Import, paclet:ref/Import])  ▪  (RefLink[Export, paclet:ref/Export])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Properties of Strings, paclet:tutorial/PropertiesOfStrings])}, {▪(RefLinkPlain[Newlines and Tabs in Strings, paclet:tutorial/NewlinesAndTabsInStrings])}, {▪(RefLinkPlain[Special Characters, paclet:tutorial/SpecialCharacters-StringsAndCharacters])}, {▪(RefLinkPlain[String-Oriented Output Formats, paclet:tutorial/StringOrientedOutputFormats])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Atomic Elements of Expressions, paclet:guide/AtomicElementsOfExpressions])}, {▪(RefLinkPlain[Layout & Tables, paclet:guide/LayoutAndTables])}, {▪(RefLinkPlain[Language Overview, paclet:guide/LanguageOverview])}, {▪(RefLinkPlain[Wolfram Language Syntax, paclet:guide/Syntax])}, {▪(RefLinkPlain[Basic Input & Output in Programs, paclet:guide/BasicInputAndOutputInPrograms])}, {▪(RefLinkPlain[WDF (Wolfram Data Framework), paclet:guide/WDFWolframDataFramework])}, {▪(RefLinkPlain[Low-Level File Operations, paclet:guide/LowLevelFileOperations])}, {▪(RefLinkPlain[Expression Structure, paclet:guide/ExpressionStructure])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-FastIntro.png(Spacer1)Fast Introduction for Programmers: Strings, http://www.wolfram.com/language/fast-introduction-for-programmers/strings/])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/33-expressions-and-their-structure.html])(WebLink[: Expressions and Their Structure, https://www.wolfram.com/language/elementary-introduction/33-expressions-and-their-structure.html])}})}})