Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Wolfshadow · Apr 24, 2017 at 01:52 AM · c#arraystring

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by bobisgod234 · Apr 24, 2017 at 01:55 AM

Use a for/foreach loop

 string newString = "";
 foreach(string s in array)
 {
     newString += s + ",";
 }
 
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arycama · Apr 24, 2017 at 11:26 AM 0
Share

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();
avatar image SohailBukhari · Apr 24, 2017 at 12:16 PM 0
Share

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);
     }
 }
avatar image Arycama SohailBukhari · Apr 24, 2017 at 12:30 PM 0
Share

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.

avatar image SohailBukhari Arycama · Apr 24, 2017 at 02:33 PM 0
Share

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.

Show more comments
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dpoly · Apr 25, 2017 at 12:06 AM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

315 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges