Display string in the UI text in specific format
Hello! I have faced specific problem and don't know how should i start working on it. There is a string, example: "Player1, please tell Player2 to call me" How can i display this string in UI text, if users have input their names so it would looks like this "Bob, please tell James to call me".
Thank you.
if they put their names in somewhere, there must be a point at which you want to change the text. is they with each character typed? three must be an onchange event somewhere. is it on a press of a button? use it's click function to call code.
the code should simply concatenate the text you already know with the variable parts, the names, and assign that result to the ui text.
Hi, thanks for your respond. $$anonymous$$ay be i'm not so clearly explain the problem. So i have string "Player1, please tell Player2 to call me" and i have two names "Bob" and "James" How can i show in UI the string so it became: "Bob, please tell James to call me" ?
Answer by UnityCoach · Feb 08, 2017 at 10:25 AM
Assuming you have a UI.Text text
, and string Player1
and string Player2
set as string variables, you can use string.Format ()
, like this :
text.text = string.Format ("{0}, please tell {1} to call me", Player1, Player2);
Yes, thank you, it worked! And i have found another way, to use string.Replace() But as i can understand string.Format() more efficien?
Yes, and you can also pass it non string types, like float or int values to display things like scores, times and such.
Your answer
Follow this Question
Related Questions
Error FormatException: Input string was not in a correct format. 0 Answers
display rotating animation while calculating operation that takes long time 0 Answers
Find with string concatenation 0 Answers
Developer Console problems..... (in-game console) 0 Answers
Windows Application : using Unity vs using native C# plus WPF 1 Answer