C# String.Format args
Solution 1
Your first example should work fine, provided there are at least two objects in the array args.
object[] args = new object[] { "Alice", 2 };
str = String.Format("Her name is {0} and she's {1} years old", args);
Solution 2
It should work just the way you want it to. The String class has the following Format method definition:
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.
Solution 3
If you do not know how many array elements are in the arguments array, try using 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.
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.
var oNewPerson = new Person(args);
console.WriteLine(oNewPerson.ToString());
This is very similar to a Microsoft example:
http://msdn.microsoft.com/en-us/library/ms173154(v=vs.80).aspx
Solution 4
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
Public Shared Function Format(ByVal format As String, ByVal ParamArray args() As Object) As String
More junk I copied from 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.
Parameters: format: A composite format string. args: An System.Object array containing zero or more objects to format.
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.
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.
-- Oops on the VB, but you get the point.
Solution 5
Both your examples do the same thing - String.Format has an overload which accepts an object[] instead of specifying each argument separately.
Les
Updated on July 09, 2022Comments
-
Les almost 2 years
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 :)
-
leppie over 14 yearsDo you have a question?
-
-
Dan Esparza over 14 yearsI love that the API definition you gave was for VB. ;-)