[英]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.