我可以像这样使用 string.Format 吗?

1 ) 将数字替换为字符串

string sample = "Hello, {name}";

//I want to put a name string into {name}, is it possible?

* *2 ) 是否可以将字符串一一添加到字符串模板中
string sample = "My name is {0}, I am living in {1} ...";

// put {0} value
// and put {1} value separately

ex)
sample[0] = "MIKE";
sample[1] = "Los Anageles";

最佳答案

1:

string sample1 = "Hello, {name}";
Console.WriteLine(sample1.Replace("{name}", "John Smith"));

2:
string sample2 = "My name is {0}, I am living in {1} ...";
var parms = new ArrayList();
parms.Add("John");
parms.Add("LA");
Console.WriteLine(string.Format(sample2, parms.ToArray()));

关于c# - 是否可以在 C# 中的 String.Format 中将索引号替换为字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10232714/

10-12 00:41
查看更多