繁体   English   中英

符号{0}是什么意思,它是什么意思?

[英]What's this symbol {0} name and what does it mean?

我找到了这个符号{0} ,这个符号是什么意思?

它最常用作字符串格式化功能的一部分,并且意味着(基于零的)列表中的第一个参数应替换它。 例如:

var output = String.Format("{0},{1}", "Hello", "World") // Gives "Hello, World"

字符串格式是数据绑定中的常见元素,因此您通常还会将其视为绑定表达式的一部分。

它是一个字符串替换标记。

看一下这个例子,它解释了这些符号的用法:

class Program
{
    static void Main()
    {
    string value1 = "Dot";
    string value2 = "Net";
    string value3 = "Perls";

    Console.WriteLine("{0}, {1}, {2}", // <-- This is called a format string.
        value1,                        // <-- These are substitutions.
        value2,
        value3);
    }
}

这将导致输出:

点,网,Perls

它可以用于字符串格式化

DateTime dat = new DateTime(2012, 1, 17, 9, 30, 0); 
string city = "Chicago";
int temp = -16;
string output = String.Format("At {0} in {1}, the temperature was {2} degrees.",
                              dat, city, temp);
Console.WriteLine(output);
// The example displays the following output: 
//    At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.   

它是一个占位符(示例):

int selectedItem = 1;

// Generate the output string
string output = string.Format("You selected item {0} from the list.", selectedItem);

Console.WriteLine(output);     // Outputs "You selected item 5 from the list."

它是复合格式字符串中从零开始的索引占位符,称为格式项。

在运行时,每个格式项都将替换为参数列表中相应参数的字符串表示形式。 如果参数的值为null,则将其替换为String.Empty

例如,对Format(String,Object,Object,Object)方法的以下调用包括具有三个格式项{0},{1}和{2}的格式字符串,以及具有三个项的参数列表。

可以在http://msdn.microsoft.com/zh-cn/library/txafckwd.aspx中找到详细的格式化帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM