- Home /
Best way to emulate Swift String Interpolation in UnityScript?
Hello,
I've been recently reading up on Swift for other projects and really like how they implement string interpolation - I have always wanted something like this for UnityScript.
Anyone know of any similar methods/options for that? It'd be really useful when writing generative dialog/storytelling, for example, to just plug vars in, rather than say, concatenating.
Thanks in advance, and all feedback welcome!
Pat
Answer by Landern · Mar 04, 2016 at 08:49 PM
use string.Format.
var someBool: Boolean = false;
var someInt: Int = 102;
var someFloat: Float = 103.0f;
function SomeFunction()
{
var aString: String = string.Format("This is a bool value: {0}\nThis is an Int value: {1}\nThis is a float value: {2}\nThis is the Int value again: {1}", someBool, someInt, someFloat);
}
The numbers between the curly braces are the index of the parameters starting with someBool in the above case, the values can be reused as their index value.
$$anonymous$$an, I really need to force Unity Answers to email me when people get back to me! Thanks a ton, I'll be looking into this when I get some free time!