- Home /
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
Answer by bobisgod234 · Apr 24, 2017 at 01:55 AM
Use a for/foreach loop
string newString = "";
foreach(string s in array)
{
newString += s + ",";
}
This will work, but if you need to do it often it will effect performance. The solution is to use a stringbuilder like so:
using System.Text;
StringBuilder sb = new StringBuilder();
foreach(string s in array)
{
sb.append(s);
}
string newString = sb.ToString();
Using Linq is best for this scenario. According to @bobisgod234 result will be same but result will iterate in all the array and return PreviousString+Currentsting. According to @Arycama Code will work fine but performance will slow because StringBuilder creates instance and then apply iteration.
using System.Linq;
using UnityEngine;
public class ArrayTest : $$anonymous$$onoBehaviour
{
[SerializeField] private string[] _stringArray;
void Start()
{
string newString = _stringArray.Aggregate<string, string>(null, (current, item) => current + item);
Debug.Log(newString);
}
}
Is there really a measurable performance difference between stringbuilder and aggregate? StringBuilder looks so much cleaner in my opinion, and I believe Aggregate is going to generate a new copy of the string each time it appends two strings together.
Aggregate() uses a loop but sb.append(s) uses Select(), which in turn uses an iterator. And using an iterator means there is some overhead: creating the iterator object and (probably more importantly) one more method invocation for each item.
Answer by dpoly · Apr 24, 2017 at 03:36 AM
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.
Answer by LilGames · Apr 25, 2017 at 05:08 AM
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.
Answer by Baroque · Apr 24, 2017 at 07:20 PM
There is the Join method on System.String for specificailly this purpose:
public static string Join(
string separator,
params string[] value
)
https://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx
No need for loops or Linq and almost certainly faster. See this question from stackoverflow: http://stackoverflow.com/questions/585860/string-join-vs-stringbuilder-which-is-faster
$$anonymous$$y solution does use String.Join(). The reason for wrapping it in Linq is to make it easier to use with a default delimiter, works on any object not just strings, handles nulls, works on any IEnumerable.
What's not to like?
BTW the Linq overhead is small -- check the source code or benchmark it.
Your answer
Follow this Question
Related Questions
C# Incrementing a String Array 1 Answer
C# String Array Has Missing or Incorrect Keycode Strings 2 Answers
How to check if the string is on a text file 0 Answers
How do I send an array over the network? 2 Answers
Only assignment, call, increment, decrement, and new object expressions can be used as a statement 1 Answer