Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

StringQ returns True both for unformatted and internally formatted strings. To get True only for unformatted ones I have thought up the following function stringQ.

Please, can you point out pitfalls, if any ? Thanks!

stringQ[str_] := Module[{ret, subStr},
    subStr = "\!\(\*\nStyleBox[\"";
    ret = Quiet[
            StringQ[str] && StringFreeQ[str, subStr]
            ,True, False, False
share|improve this question
what is "internally formatted string"? A string is a string. Why would it matter what is inside it? I did not know that Mathematica had 2 kinds of strings before. – Nasser Mar 25 '14 at 1:04
@Nasser: Terminology straight from the documentation... – ciao Mar 25 '14 at 1:10
@rasher, ok, I just never heard or seen something called "internally formatted string" reading Mathematica documentation. I just googled this and it turned no hit. But may be I just need to look harder. – Nasser Mar 25 '14 at 1:16
@Nasser: See scope under StringQ documentation. – ciao Mar 25 '14 at 1:28
@rasher thanks, I see it now. But for me it is just a string. Anything with a starting " and ending " is a string. But I understand now the OP wanted to parse/detect it to see if it it contains Mathematica specific content. Ok. – Nasser Mar 25 '14 at 1:34
up vote 4 down vote accepted

You method seems fine. Here are some alternatives:

stringFQ[string_String] := Module[{str, res},
   res = Read[str = StringToStream[string], String];
   res == string];

stringFQ2[string_String] := StringFormat[string] != "Binary"

stringFQ3[string_String] := ExportString[string, "Text"] === string

stringFQ4[string_String] := string == ToString[string]

For huge strings, the first can be 10+X faster on those containing format constructs.

The second is about as concise as it can get I suppose, see docs for heuristics caveats.

Third is nearly as compact, no caveats other than slower than others on huge strings.

Fourth is also compact, about same speed as others for large strings.

So, many ways to skin this cat, depends on your needs for speed...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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