c# - 是否可以两次使用占位符字符串?

标签 c# string placeholder

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Type a number");
            ConsoleKeyInfo KeyInfo1 = Console.ReadKey();
            Console.WriteLine("Type another number");
            ConsoleKeyInfo KeyInfo2 = Console.ReadKey();
            Console.WriteLine("The number is {0}", KeyInfo1.KeyChar.ToString() + "The time is {1}. Is this right? Press y for yess or n for no.", KeyInfo2.KeyChar.ToString());
            Console.ReadKey();
        }
    }
}

我问这个问题的原因是第一个数字显示在控制台中,但第二个数字没有显示,它只是显示 {1}。我希望这是有道理的,我是编程新手。

最佳答案

WriteLine 函数的第一个参数是格式字符串。
那是您添加占位符的部分。
在第一个格式参数之后添加值以替换占位符作为参数,如下所示:

Console.WriteLine("The number is {0} The time is {1}. Is this right? Press y for yess or n for no.", KeyInfo1.KeyChar.ToString(), KeyInfo2.KeyChar.ToString());

第一个参数(在格式之后,即传入函数的第二个参数)将替换 {0},第二个参数将替换 {1}

关于c# - 是否可以两次使用占位符字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29046672/

相关文章:

c# - 删除或替换,当我从字符串中删除子字符串时,哪个更好?

css - 为什么 CSS 文本颜色会覆盖 IE 10 中的占位符颜色?

jquery - 如何在 JQuery Select2 插件中使用选定值创建占位符文本

c# - 有什么方法可以确定在 C# 中等待锁定的线程数?

c# - 如何从托管 (C#) 数组(例如 `NSArray` s)中创建 `int`?

Javascript 匹配以从 URL 中删除部分文件名 - 替换最后一次出现

java - 将一个字符串分成两部分

css - 在所有浏览器中覆盖占位符字体 css

c# - Entity Framework - 手动添加导航属性

c# - VSTO : Open a Word document which is located in another directory than the VSTO file