Search code examples
c#stringwinformsformattingtextbox

I can't get my string to display properly


So basically, the below code should display the following. "This text variable1 then this text variable2 followed by this text"

textBox1.Text = string.Format("This text {0} then this text {1} followed by this text", variable1, variable2);

This instead display the following "This text variable1 variable2 then this text followed by this text"


Solution

  • I cannot see how two variables could be squeezed into one placeholder. Something else must be going on.

    Does variable1 contain all the text that was supposed to be split among variable1 and variable2?


    String interpolation can make things easier and you can't confuse the indices:

    textBox1.Text = $"This text {variable1} then this text {variable2} followed by this text";
    

    You would use String.Format only if the format was supplied as a variable. If it is supplied as a string literal, use string interpolation.