如何“注入"细绳 [英] how to "inject" string

查看:44
本文介绍了如何“注入"细绳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我不确定如何描述自己(不确定这个问题的名字是什么)

我可以这样写到控制台

Console.WriteLine("The answer of {0} + {1} is {2}",2,2,2+2);

这个主题({0} ...和参数)的确切称呼是什么?



无论如何,我想知道我是否可以写这样的东西.



Hi,

i''m not sure how to describe my self (not sure what is the name of this issue)

i can write to console like this

Console.WriteLine("The answer of {0} + {1} is {2}",2,2,2+2);

how exactly this topic (of {0}... and the arguments) is called ?



any way, i was wondering if i can write something like this.



Dictionary <string,int> dic = new ...

string [] arr = {"hello","world"};

foreach (var str in arr)
{
dic.Add({0},str, str.Length);
}


我知道这不是正确的语法,但我希望我可以理解...:)


I know that it''s not the correct syntax, but i hope i was understandable enough... :)

推荐答案

我们可以格式化string array 并添加回Dictionary,请参见下面的代码,

We could format item from string array and add back to the Dictionary, please see below code,

public static IDictionary<string, int> StringFormmater = new Dictionary<string, int>()
{
};

static void Main(string[] args)
{
    string[] stringArray = { "C# {0} and {1}", "FrameWork {0} and {1}" };
    Array.ForEach(stringArray, item =>
    {
        StringFormmater.Add(string.Format(item, "Injected","From For loop"), item.Length);
    });

}



:)



:)


dic.Add(string.Format("{0}",str),str.Length);
dic.Add(string.Format("{0}", str), str.Length);


这篇关于如何“注入"细绳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆