首页 > 解决方案 > 无法将类型 '(string, int USD) 隐式转换为 'string'

问题描述

我一直在努力解决我想将数组的变量放在另一个数组的空变量中但在使用 FOR LOOP 时格式化的问题。我遇到的错误是

无法将类型“(字符串,字符串)隐式转换为“字符串”

我不能以这种方式实现它还是我在做一些愚蠢的事情?

对于背景,我正在尝试格式化string[]数组中的字符串,所以它会像这样:

----------------------------------------------------
9 USD: [Base coins: 450] [Added coins: 0] 
[Total coins: 450]

12 USD: [Base coins: 600] [Added coins: 0] 
[Total coins: 600]

----------------------------------------------------

但是通过使用 Alignment 组件进行了格式化,因此由于数字更长/更短,它没有那种奇怪的间距:

("{0, -5}", string[0]); //This is not the full code, just a snippet

任何帮助将不胜感激!

(直到发布此帖子后 20 分钟后,我可能会意识到这个愚蠢的问题)

    int USD = 3;
    int coins = 150;
    int extraCoins = 0;

    string[] names = { "USD", "Base coins", "Added coins", "Total coins" };
    char[] specialCharacters = { '[', ']', ':' };   // Used to indent strings

    string[] numbers = new string[iterations + 1];  // Iterations is a global variable set to 10

    for (int i = 1; i < numbers.Length; i++)
    {
        //numbers[i] = ($"{USD * i} USD: [Base coins: {_coins * i}] [Added coins: {extraCoins * i}] \n[Total coins: {(_coins * i) + (extraCoins * i)}]");
        numbers[i] = ("{0,-5}", string[0]); //THIS IS THE PROBLEM >:(
    }

    return numbers;

标签: c#

解决方案


您需要使用字符串插值或 String.Format

 numbers[i] = $"{string[0],-5}";

或者

 number[i] = String.Format("{0,5}",string[0]); 

推荐阅读