C# String.Format 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1434059/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:29:10  来源:igfitidea点击:

C# String.Format args

c#.netstringformatting

提问by Fabio Milheiro

I have an array like this:

我有一个这样的数组:

object[] args

and need to insert those args in a string, for example:

并且需要在字符串中插入这些参数,例如:

str = String.Format("Her name is {0} and she's {1} years old", args);

instead of:

代替:

str = String.Format("Her name is {0} and she's {1} years old", args[0], args[1]);

NOTE: Actually the first line of code worked! But args[1] was missing! Sorry and thank you. Points for every one :)

注意:实际上第一行代码有效!但是 args[1] 不见了!对不起,谢谢。每人加分:)

采纳答案by csharptest.net

Your first example should work fine, provided there are at least two objects in the array args.

您的第一个示例应该可以正常工作,前提是数组 args 中至少有两个对象。

object[] args = new object[] { "Alice", 2 };
str = String.Format("Her name is {0} and she's {1} years old", args);

回答by Stephen

It should work just the way you want it to. The String class has the following Format method definition:

它应该按照您希望的方式工作。String 类具有以下 Format 方法定义:

public static string Format(string format, params object[] args);

Seeing as how the "args" in your example is an array of objects, it should fit right in.

看到您的示例中的“args”是一个对象数组,它应该适合。

回答by sshow

Your second code-block would do what I think you are trying to accomplish.

你的第二个代码块会做我认为你想要完成的事情。

string.Format("Hello {0}, {1} and {2}", new object[] { "World", "Foo", "Bar" });

回答by leppie

Did you even try the first line? Did you see that it should work the same as the second?

你甚至尝试过第一行吗?你看到它应该和第二个一样工作吗?

回答by Henk Holterman

str = String.Format("Her name is {0} and she's {1} years old", args);

Should work when args is of type object[].

当 args 是 object[] 类型时应该可以工作。

回答by C???

I'm not sure what you're asking, but either of those should work, considering that one of the signatures for the String.Format() function is

我不确定你在问什么,但考虑到 String.Format() 函数的一个签名是

Public Shared Function Format(ByVal format As String, ByVal ParamArray args() As Object) As String

More junk I copied from Visual Studio:

我从 Visual Studio 复制的更多垃圾:

Summary:Replaces the format item in a specified System.String with the text equivalent of the value of a corresponding System.Object instance in a specified array.

摘要:将指定 System.String 中的格式项替换为指定数组中对应 System.Object 实例的值的文本等效项。

Parameters:format: A composite format string. args: An System.Object array containing zero or more objects to format.

参数:format:复合格式字符串。args:包含零个或多个要格式化的对象的 System.Object 数组。

Return Values:A copy of format in which the format items have been replaced by the System.String equivalent of the corresponding instances of System.Object in args.

返回值:格式项已被替换为与 args 中 System.Object 的相应实例等效的 System.String 的格式副本。

Exceptions:System.ArgumentNullException: format or args is null. System.FormatException: format is invalid. -or- The number indicating an argument to format is less than zero, or greater than or equal to the length of the args array.

异常:System.ArgumentNullException:格式或参数为空。System.FormatException:格式无效。- 或 - 指示 format 参数的数字小于零,或大于或等于 args 数组的长度。

-- Oops on the VB, but you get the point.

-- 糟糕,VB,但你明白了。

回答by Lee

Both your examples do the same thing - String.Format has an overload which accepts an object[] instead of specifying each argument separately.

你的两个例子都做同样的事情 - String.Format 有一个重载,它接受一个 object[] 而不是分别指定每个参数。

回答by Rusty Naumann

If you do not know how many array elements are in the arguments array, try using string.Join().

如果您不知道 arguments 数组中有多少数组元素,请尝试使用 string.Join()。

string.Format("Arguments passed in to the program are: {0}", string.Join(" ", args));

Specifically in your example:

具体在您的示例中:

string.Format("Her name is {0} years old", string.Join(" and she's ", args));

Personally, I don't like hard-coded structures of an array object. That's too much to remember throughout the application and makes it hard to maintain. I would rather turn the arguments into a "Person" object with a constructor that accepts the array, and overload the ToString() to display the specific information about the object members.

就个人而言,我不喜欢数组对象的硬编码结构。在整个应用程序中要记住太多,并且很难维护。我宁愿将参数转换为具有接受数组的构造函数的“Person”对象,并重载 ToString() 以显示有关对象成员的特定信息。

class Person
{
    private string m_sName;
    private string m_sAge;

    public Person(string[] args)
    {
        m_sName = args[0];
        m_sAge = args[1];
    }

    public override string ToString()
    {
        return string.Format("Her name is {0} and she's {1} years old.", m_sName, m_sAge);
    }
}

So you can construct a "Person" object and display the message when called.

因此,您可以构造一个“Person”对象并在调用时显示消息。

var oNewPerson = new Person(args);
console.WriteLine(oNewPerson.ToString());

This is very similar to a Microsoft example:

这与 Microsoft 示例非常相似:

http://msdn.microsoft.com/en-us/library/ms173154(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/ms173154(v=vs.80).aspx