简体   繁体   English

C#String.Format args

[英]C# String.Format args

I have an array like this: 我有这样一个数组:

object[] args

and need to insert those args in a string, for example: 并且需要在字符串中插入这些args,例如:

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! 但args [1]失踪了! Sorry and thank you. 对不起,谢谢。 Points for every one :) 每一个点:)

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);

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”是一个对象数组,它应该适合。

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

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.Object实例的值等效的文本替换指定System.String中的格式项。

Parameters: format: A composite format string. 参数: format:复合格式字符串。 args: An System.Object array containing zero or more objects to 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.ArgumentNullException:format或args为null。 System.FormatException: format is invalid. System.FormatException:格式无效。 -or- The number indicating an argument to format is less than zero, or greater than or equal to the length of the args array. - 或 - 指示格式参数的数字小于零,或大于或等于args数组的长度。

-- Oops on the VB, but you get the point. - 在VB上哎呀,但你明白了。

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

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

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

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

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

Did you even try the first line? 你有没有试过第一线? Did you see that it should work the same as the second? 你有没有看到它应该和第二个一样?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM