Group Abstract Group Abstract

Message Boards Message Boards

0
|
6.1K Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Evaluate an expression contained within a string?

Posted 5 years ago

I'm new to Mathematica and to this forum. I'm experienced in some other languages. I'm running version 12.1 on Windows 10

I'd like to be able to build expressions in a string and then evaluate/execute those expressions, e.g. as in the following imaginary code:

In[10]:= myExpr = "2+2";

In[11]:= evalString[myExpr] 

Out[11]: = 4 

In[12]:= evalString["Range[5]"] 

Out[12]:= {1, 2, 3, 4, 5}

I thought I was onto something with WSEvaluateString, but it seems this is not what I want.

Can I manipulate expressions in this way and then execute them?

Thanks.

POSTED BY: Chas Int
3 Replies
Posted 5 years ago

Many thanks - exactly what I was looking for!

POSTED BY: Chas Int

Have you tried ToExpression? It takes a string and converts it to an expression.

POSTED BY: Nathan Shpritz

The built-in function ToExpression does the job.

ToExpression["2+ 2"]

ToExpression["Range[5]"]
POSTED BY: Ta'a Nwa Dombou
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard