Message Boards Message Boards

4 Replies
4 Total Likes
View groups...
Share this post:

Variable Inside a String

Posted 11 years ago
I am creating a simple text-based game in Mathematica.

I am having a slight trouble with putting variables inside texts.
I tried,
{"Hey!" name "Do you want to play a game?"}
Where name is a variable for a character name the user specified before using
InputField[Dynamic[name], String]
When the set of strings above are computated, they fall out in an alphabetical order, making no sense at all.

Is there a simple way to put a variable in a string?

Also, if you have any advise on what I can do with a text-based game, it would be welcome.

Thank you.
POSTED BY: Seokin Yeh
4 Replies
You cannot simple put a variable in between two strings because this is interpreted as an invisible multiplication by Mathematica. This is basically the same as when you would write c = a b. To see what happens you just have to look at the FullForm of the expression you entered
name = "ME";
"Ace" name "Corperation" // FullForm

(* Times["Ace","Corperation","ME"] *)

What you want to do is to use StringJoin
"Ace" <> name <> "Corperation"
POSTED BY: Patrick Scheibe
Row is also very useful: 
Row[{"Hey! ", name, ", Do you want to play a game?"}]

If you copy the output to a new notebook, you can have a app-like interface. 

POSTED BY: Shenghui Yang
Thank you very much for your help.
POSTED BY: Seokin Yeh
I have never thought of doing that!

Thank you very much!
POSTED BY: Seokin Yeh
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract