21
$\begingroup$

The documentation on String states:

Strings preserve internal formatting

But if I try to perform some manipulations on this string, for example, take 2 words from the left, I get some shards of internal representation instead (which is understandable):

(* In[2]:= *) StringTake[%1, 8]
(* Out[2]= "\!\(\*\nStyl" *)

Question: Is it possible to write a function which strips all formatting (colors, fonts, style, size, etc) from a string and returns just a plain text?

$\endgroup$
2
  • 2
    $\begingroup$ Somewhat related: (7732), (10990) $\endgroup$
    – Mr.Wizard
    Aug 4, 2013 at 22:16
  • 2
    $\begingroup$ Thanks for the Accept, but I recommend all users wait 24-hours to let everyone have a chance to answer. You may like another answer more if you give it a chance to happen. $\endgroup$
    – Mr.Wizard
    Aug 4, 2013 at 22:43

1 Answer 1

24
$\begingroup$

For most strings you can use simply:

ToString[string]

Or more rigorously:

ToString[string, OutputForm]

Observe:

enter image description here


If your styled string includes two dimensional formatting that you do not wish to change you will find OutputForm unacceptable. Of course the 2D formatting itself will mean that the string cannot be in a "plain" form, but we can still strip styling directives such as size, slant, and color. Here is a rather baroque way to deal with that. I will use the gloriously-named UndocumentedTestFEParserPacket described here by John Fultz. I will then convert these Boxes back into a string and strip a remnant TagBox (using StringTake) to produce the cleanest string possible.

parseString[s_String, prep : (True | False) : True] := 
  FrontEndExecute @ FrontEnd`UndocumentedTestFEParserPacket[s, prep]

cleanString[s_String] :=
  ToString[DisplayForm@parseString[s, True][[1, 1]], StandardForm] //
    "\!" <> StringTake[#, {11, -16}] &

Observe:

enter image description here

$\endgroup$
1
  • $\begingroup$ Could this be changed to filter out all boxes, like SuperscriptBox and SubscriptBox? $\endgroup$
    – Lucent
    Aug 19, 2020 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.