({{, (RefLink[ToString, paclet:ref/ToString])[expr]
gives a string corresponding to the printed form of expr in (RefLink[OutputForm, paclet:ref/OutputForm]). }, {, (RefLink[ToString, paclet:ref/ToString])[expr,form]
gives the string corresponding to output in the specified form. }})

OpenersNotesSection(Spacer1)Details and Options

The following options can be given:

(RefLink[ToString, paclet:ref/ToString]) uses any relevant definitions given for (RefLink[Format, paclet:ref/Format]) and (RefLink[MakeBoxes, paclet:ref/MakeBoxes]). »

OpenersPrimaryExamplesSection(Spacer1)Examples  (15)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Give a string corresponding to the (RefLink[OutputForm, paclet:ref/OutputForm]) of (x^2):

ToString[(x^2)]

Head[%]

Give a string corresponding to the (RefLink[InputForm, paclet:ref/InputForm]):

ToString[(x^2),InputForm]

OpenersExampleSection1(Spacer1)Scope  (2)

Strings for (RefLink[OutputForm, paclet:ref/OutputForm]), (RefLink[InputForm, paclet:ref/InputForm]), (RefLink[StandardForm, paclet:ref/StandardForm]), and (RefLink[TraditionalForm, paclet:ref/TraditionalForm]) of an expression:

ToString[x^2+y^3, OutputForm]

ToString[x^2+y^3, InputForm]

ToString[x^2+y^3, StandardForm]

ToString[x^2+y^3, TraditionalForm]

Non-default character encoding:

ToString[α,StandardForm]

ToString[α,StandardForm,CharacterEncoding->"ASCII"]

OpenersExampleSection2(Spacer1)Options  (4)

OpenersExampleSubsection0(Spacer1)CharacterEncoding  (1)

Generate a string using the default encoding:

ToString[α]

Use ASCII encoding instead:

ToString[α,CharacterEncoding->"ASCII"]

OpenersExampleSubsection1(Spacer1)FormatType  (1)

Use (RefLink[FormatType, paclet:ref/FormatType]) to change the formatting:

ToString[Exp[x]]

ToString[Exp[x],FormatType->StandardForm]

OpenersExampleSubsection2(Spacer1)NumberMarks  (1)

Use the default setting of (RefLink[InputForm, paclet:ref/InputForm]) formatting:

ToString[1.2,InputForm]

Explicitly set whether number marks should be included:

{ToString[1.2,InputForm,NumberMarks->True],ToString[1.2,InputForm,NumberMarks->False]}

OpenersExampleSubsection3(Spacer1)PageWidth  (1)

By default, the page width is infinite:

ToString[Cos[x]+Cos[y]]

Wrap to a new line after 9 characters in the string:

ToString[Cos[x]+Cos[y],PageWidth->10]

OpenersExampleSection3(Spacer1)Applications  (2)

Construct an indexed set of variables:

v=ToExpression["x"<>ToString[#]]&/@Range[5]

x1=5;v

Export data to files with programmatically created names:

v=Map[{#,#[1.]}&,{Sin,Cos,Tan,Cot,Exp}]

f[x_]:=Export[FileNameJoin[{$TemporaryDirectory,ToString[x[[1]]]<>".txt"}],x[[2]]]

ofiles=f/@v;

StringTake[#,-7]&/@ofiles

Import/@ofiles

OpenersExampleSection4(Spacer1)Properties & Relations  (4)

Use (RefLink[ToExpression, paclet:ref/ToExpression]) to convert strings or boxes to expressions:

ToString[x^3,StandardForm]

ToExpression[%]

Use (RefLink[ToBoxes, paclet:ref/ToBoxes]) to convert expressions to boxes:

ToBoxes[%]

Use (RefLink[ToString, paclet:ref/ToString]) with (RefLink[ToExpression, paclet:ref/ToExpression]) to convert TeX to Wolfram Language syntax:

t=TeXForm[1.2 x+Exp[y]]

ToExpression[ToString[t],TeXForm]

Use (RefLink[StringLength, paclet:ref/StringLength]) to find a string's length:

s=ToString[FullForm[α β]]

StringLength[s]

Use (RefLink[Characters, paclet:ref/Characters]) to extract the individual characters:

Characters[s]

(RefLink[ToString, paclet:ref/ToString]) uses definitions from (RefLink[Format, paclet:ref/Format]) and (RefLink[MakeBoxes, paclet:ref/MakeBoxes]):

ToString[kappa]

Format[kappa]:=κ

ToString[kappa]

ToString[gplus[a,b,c],StandardForm]

gplus/:MakeBoxes[gplus[x_,y_,n_],StandardForm]:=RowBox[{MakeBoxes[x,StandardForm],SubscriptBox["⊕",MakeBoxes[n,StandardForm]],MakeBoxes[y,StandardForm]}]

ToString[gplus[a,b,c],StandardForm]

OpenersExampleSection5(Spacer1)Possible Issues  (1)

(RefLink[ToString, paclet:ref/ToString]) by default uses (RefLink[OutputForm, paclet:ref/OutputForm]) formatting:

ToString[u+Style[i,Red]]

Specify other formats to preserve styling information:

ToString[u+Style[i,Red],StandardForm]

ToString[u+Style[i,Red],TraditionalForm]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ToBoxes, paclet:ref/ToBoxes])  ▪  (RefLink[ToExpression, paclet:ref/ToExpression])  ▪  (RefLink[HoldForm, paclet:ref/HoldForm])  ▪  (RefLink[WriteString, paclet:ref/WriteString])  ▪  (RefLink[TextString, paclet:ref/TextString])  ▪  (RefLink[SymbolName, paclet:ref/SymbolName])  ▪  (RefLink[StringTemplate, paclet:ref/StringTemplate])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[The Representation of Textual Forms, paclet:tutorial/TheRepresentationOfTextualForms])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Converting between Expressions & Strings, paclet:guide/ConvertingBetweenExpressionsAndStrings])}, {▪(RefLinkPlain[String Manipulation, paclet:guide/StringManipulation])}, {▪(RefLinkPlain[Character Operations, paclet:guide/CharacterOperations])}, {▪(RefLinkPlain[Symbol Handling, paclet:guide/SymbolHandling])}, {▪(RefLinkPlain[Text Generation, paclet:guide/TextConstruction])}})}})

({{(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-NKS.png(Spacer1)NKS|Online, http://www.wolframscience.com/nksonline/index/search.cgi?SearchIndex=ToString]) ((WebLink[A New Kind of Science, http://www.wolframscience.com/nksonline/]))}})}})