change variable from textAsset in dictionary
Hi, i've been working in a localization system, i read a txt file from the resources folder, and save it in a dictionary, and i retrieve the value(a string), it works pretty fine, but it's possible to do something like: "dummy text" + variable+" more text" and get the value of the variable from the script?
thanks in advance!
Are you asking if there is a way to come up with a single string containing some predefined text AND the variable in the middle of that string? Because since they are all strings there is no problem with using the + operator that you mentioned, to come up with a concatenated string.
Answer by jgodfrey · May 01, 2016 at 04:24 PM
You probably want to prepare your strings for processing via the String.Format method. As an example, you could have the following "Hello" strings in your resource file:
"Hello {0}"
"Hola {0}"
Assuming you load one of those localized strings into a variable named "helloString", you could insert someone's name into the string like this:
string finalString = String.Format(helloString, name);
Debug.Log(finalString);
Where "name" contains the players name (for example):
If your string needs multiple "variables", just include them as necessary. For example, a string like this:
answerString = "Sorry {0}. The correct answer is {1}.";
Could be "filled out" via String.Format like:
string finalString = String.Format(answerString, name, answer);
Debug.Log(finalString);
Where "name" contains the player name and "answer" contains the answer...
The magic is all in the String.Format method. More details can be found here:
https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx
Your answer
Follow this Question
Related Questions
Insert string into empty list at a specific index 0 Answers
Convert keycode to string 1 Answer
Set a variable from another script 1 Answer
Interchangeable animations in a script? 0 Answers
Vector2.MoveTowards Not Working 1 Answer