0

I would like to output all objects in an array of strings for which the length is variable.

example 1 :
string id = A;
string [] values = new string [] {"12","23"};
string output = string.Format("{0}, {1}, id, values);
//output should be "A,12,23"

example 2 :
string id = A;
string [] values = new string [] {"12","23","45","67","89"};
string output = string.Format("{0}, {1}, id, values);
//output should be "A,12,23,45,67,89"

Is there any way which would cover any number of values or do I need to convert the values into a String and then output it?

1
  • 2
    There are some overloads of string.Join that you can try... They are present in C# >= 4.0 and they work with arrays and IEnumerable<> Commented Jun 26, 2015 at 11:08

1 Answer 1

3

You could do it easily with a string.Join(string, string[])

string.Join(",", values)

it will add the separator char and will output it in the way you want it

An overview of the methods you could use for it are here:

https://msdn.microsoft.com/en-us/library/System.String.Join%28v=vs.110%29.aspx

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.