2
$\begingroup$

When trying to make a string an expression I see some wrong results:

str = "one Test String to see"
(* "one Test String to see" *)

str //ToExpression
(* one see String Test to *)

What is the correct way to do that to avoid the mess up?

Thanks!

UPDATE

I am expecting to have following output:

one Test String to see

UPDATE 1

As @ Mr.Wizard said

ToExpression[str, StandardForm, HoldForm]

Does what I want. Now if I have two strings (lot's of in reality)

{"one Test String to see", "300/1"}

How can I do to perform ToExpression[str] on the element that contains numbers, and ToExpression[str, StandardForm, HoldForm] on the one that is string only?

UPDATE 2

To be clear let me bring an example. Let's say I have following string:

a = "{a -> some String here, b -> 3/5, c -> {1, 2, 4, 6}}"
(* "{a -> some String here, b -> 3/5, c -> {1, 2, 4, 6}}" *)

Now I want to bring this to list of rules to have following output (nothing is code here)

{a -> here some String, b -> 3/5, c -> {1, 2, 4, 6}}

$\endgroup$

closed as unclear what you're asking by Mr.Wizard Apr 2 '15 at 14:26

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ It is unclear what you are asking. What output do you expect? If you want to avoid the sorting due to Orderless on Times you will need a hold function, e.g. ToHeldExpression or MakeExpression. (Or to hide the hold function ToExpression[str, StandardForm, HoldForm].) However these may still not do what you want, whatever that is, as other kinds of parsing still take place. $\endgroup$ – Mr.Wizard Mar 29 '15 at 10:55
  • $\begingroup$ Regarding the update are all the elements of the "string only" part valid Symbols, while the other contains elements that are not Symbols? If so we can use that to separate the types. If not this needs further clarification. $\endgroup$ – Mr.Wizard Mar 29 '15 at 11:31
  • $\begingroup$ @Mr.Wizard In my case only "string only" part needs to be parsed "with hold", the other parts need to be parsed with ToExpression[], since they are only numbers and letters like "2/3 cm" or {1, 2, 3} $\endgroup$ – SuTron Mar 29 '15 at 11:36
  • $\begingroup$ 2/3 cm contains cm which would be parsed as a Symbol; {1, 2, 3} would be parsed as a List; is this correct? Please see my comment below the answer also. $\endgroup$ – Mr.Wizard Mar 29 '15 at 11:38
  • $\begingroup$ The input shown in UPDATE 2 is not a String. Your output is still ambiguous, unless you actually want a bitmap image as output. Please provide the real input and real output that you want it to produce. $\endgroup$ – Mr.Wizard Mar 29 '15 at 12:20
3
$\begingroup$

Perhaps the closest thing to what you want is to use HoldForm to prevent the sorting behavior of Times due to Orderless:

str = "one Test String to see";

HoldForm @@ MakeExpression @ str
one Test String to see

However it is important to realize that the HoldForm head is still present, only that it is not shown in standard formatted output. Also know that the string is still parsed and the string must be valid syntax:

HoldForm @@ MakeExpression @ "one Test String to see 2|~+7"
HoldForm[ErrorBox["one Test String to see 2|~+7"]]

If your goal is to insert an expression into the Notebook then try:

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

Cell[parseString[str][[1]], "Input"] // CellPrint
one Test String to see

This time that output actually is live code in an Input cell.

parseString is a method from John Fultz posted here:

Related:

$\endgroup$
  • $\begingroup$ Thanks for the answer! Is there a way to have the result not as a code, but as a string. This also doesn't solve Update1, since "3/5" form string will still stay "3/5" $\endgroup$ – SuTron Mar 29 '15 at 11:30
  • $\begingroup$ @SuTron I repeat: it is not clear what you want to do. str starts as a string, so how can it be the output also? Are you trying to evaluate mathematical expressions within a string and replace them in the string with their evaluated forms? $\endgroup$ – Mr.Wizard Mar 29 '15 at 11:37

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