c# - 无法将类型 '(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;
解决方案
您需要使用字符串插值或 String.Format
numbers[i] = $"{string[0],-5}";
或者
number[i] = String.Format("{0,5}",string[0]);
推荐阅读
- r - 如何将长 LaTeX 表拆分为相邻列?
- android - Android - 一个一个地在 xml 中为文本视图设置动画
- amazon-s3 - 德鲁伊中的 AWS S3 连接
- python - 使用 BeautifulSoup 从表格中提取彩色文本
- java - 标题中带有嵌套可扩展列表的 Recycleview
- asp.net-core - 在现有 ASP.NET Core MVC 应用程序中实现服务工作者
- sql - SQL Loader Unix - 给出错误 SQL Loader-500 无法打开文件 (ABC_CTL.dat)
- flutter - 将 mvc_pattern 库更新到最新版本 6.4.0 后获取上下文的 Flutter 错误
- php - Laravel 更改 URL 名称详细信息
- bash - 复制粘贴代码有效,但不能作为脚本