Using an array as argument for string.Format()

后端 未结 4 1773
甜味超标
甜味超标 2021-01-07 19:13

When trying to use an array as an argument for the string.Format() method, I get the following error:

FormatException: Index (zero based)

4条回答
  •  清酒与你
    2021-01-07 20:01

    You can convert int array to string array as pass it using System.Linq Select() extension method.

    infoText.text = string.Format("Player1: {0} \nPlayer2: {1} \nPlayer3: {2} \nPlayer4: {3}", 
                                  place.Select(x => x.ToString()).ToArray());
    

    Edit:

    In C# 6 and above, you can also able to use String Interpolation instead of using string.Format()

    infoText.text = $"Player1: {place[0]}\nPlayer2: {place[1]} \nPlayer3: {place[2]} \nPlayer4: {place[3]}";
    

    Check this fiddle for your reference.

提交回复
热议问题