1

I want to create a method that accepts a string MyString and a List of strings MyList.

MyString contains placeholders like {0}, {1} and so on... but I don't know in advance how many. Example: "I want to put something here: {0} and something there: {1}"

MyList contains the strings that must be substituted in MyString, in their correct place: the first string, with index 0, must be put in the placeholder {0}.

I would like to achieve this with String.Format but I can't understand how to pass MyString as a list of arguments.

Is this possible in VB.NET?

Thank you

1 Answer 1

1

Just convert your list to an array, then you're calling this overload:

string result = string.Format(formatString, list.ToArray());

For example:

string formatString = "I want to put something here: {0} and something there: {1}";
var list = new List<string> { "foo", "bah" };
string result = string.Format(formatString, list.ToArray());
// Result:  I want to put something here: foo and something there: bah

If you pass your list the wrong overload of String.Format is used.


Sorry for C#, here VB.NET:

Dim result = String.Format(formatString, list.ToArray())
Sign up to request clarification or add additional context in comments.

1 Comment

WOW! this was so damn simple but I really didn't even look for an overload! Thankx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.