3

I'm having a List, it contains the Person's Information

C# Code:

List<string> Person = new List<string>() {"Watson", "1001", "Female"};

My Expected String should be

string format = @"Name: {0}({1}) - {2}";
string expectedString = string.Format(format, ......);

I wish to LOOP the List<string> Person as an argument within the method string.Format()

I need to format the string in dynamic not by index number (i.e., static).

The Output should be

string expectedString = "Name: Watson(1001) - Female";
0

1 Answer 1

10

You can use the string.Format overload that takes a params object array argument, if the list order is guaranteed.

List<string> Person = new List<string>() {"Watson", "1001", "Female"};
string format = @"Name: {0}({1}) - {2}";
string expectedString = string.Format(format, Person.ToArray());

This outputs Name: Watson(1001) - Female

Sign up to request clarification or add additional context in comments.

Comments

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.