Access all Items in an Array

Hello everyone,

I need to be able to turn a bunch of strings in an array into 1 string (with commas in between each entry). I guess pseudocode would look like:

void Update ()

    {
    string newString = Array [1] + "," + Array [2] + "," + Array [3] etc
    }

But I need to automate this, as I don’t want to write out above for 200 array entries or so. Any ideas?
Thanks,
Wolfshadow

Use a for/foreach loop

string newString = "";
foreach(string s in array)
{
    newString += s + ",";
}

Use an extension method. Here is mine:

using System.Linq;
    
// string join that works on any enumerable
public static class UtilExtensions {
  public static string Join<T>(this IEnumerable<T> values, string delim = ",") {
      return String.Join(delim, values.Select(v => v == null ? "null" : v.ToString()).ToArray());
   }
}
    
    void Update () {
        string newString = SomeArray.Join();
    }

This code works and it is idiomatic C#. Study it, and when you understand it, use it everywhere. I do.

Look up “for” loops.

https://unity3d.com/learn/tutorials/topics/scripting/loops

Make a loop that lasts as long as your array length, and then you add each array entry to your string variable.

There is the Join method on System.String for specificailly this purpose:

public static string Join(
	string separator,
	params string[] value
)

No need for loops or Linq and almost certainly faster. See this question from stackoverflow: .net - String.Join vs. StringBuilder: which is faster? - Stack Overflow