C#如何将放置变量添加到资源字符串中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1008966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# How to add placement variable into a resource string
提问by scope_creep
This should be easy, but can't find anything to explain it.
这应该很容易,但找不到任何东西来解释它。
Say I am writing something out on console.writeln like:
假设我在 console.writeln 上写了一些东西,比如:
console.writeln("Jim is a {0} ", xmlscript);
console.writeln("Jim is a {0} ", xmlscript);
Say I wanted to convert string `"Jim is.." to a resource string in a global resource.resx. It would be:
假设我想将字符串 `"Jim is.." 转换为全局 resource.resx 中的资源字符串。这将是:
jimstring jim is a {0}
jimstring jim is a {0}
and I would refer to it in code as
我会在代码中将其称为
console.writeln(Resources.jimstring)
console.writeln(Resources.jimstring)
How to I put the placement variable (xmlscript
) (is this what they are called?) into the resource string in console.writeln?
如何将放置变量 ( xmlscript
)(这是它们的名称吗?)放入 console.writeln 中的资源字符串中?
Thanks,
谢谢,
Bob
鲍勃
采纳答案by Sailing Judo
As Jeff Johnson mentioned in his answer, it basically the exact same thing as the original Console.WriteLine(). The resource string is just a string. So you reference the resource file and do the format.
正如杰夫约翰逊在他的回答中提到的,它基本上与原始 Console.WriteLine() 完全相同。资源字符串只是一个字符串。所以你引用资源文件并进行格式化。
If you need it for something other than the Console you can use the String.Format():
如果您需要它用于控制台以外的其他东西,您可以使用 String.Format():
var newString = String.Format(resources.jimstring, xmlscript);
回答by jjxtra
Console.WriteLine(Resources.jimstring, xmlscript);
Console.WriteLine takes additional formatting arguments that will replace the {0} in your Resources.jimstring string.
Console.WriteLine 采用额外的格式参数来替换 Resources.jimstring 字符串中的 {0}。
More info here: http://msdn.microsoft.com/en-us/library/828t9b9h.aspx