Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
75 views
in Technique[技术] by (71.8m points)

.net - C# String.Format args

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

question from:https://stackoverflow.com/questions/1434059/c-sharp-string-format-args

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...