0

I'm new in c# unity. I've to save my array of positions inside firebase for that I'm creating an int array like

int[] positions = new int[] {2, 4, 3};

its working fine but I don't know how can I convert this into string array like "[2, 4, 3]" to save in firebase.

I've searched on google and tried

string stringPositions = string.Join("", positions);

but it completely converts my array into a string like 234. And also how can I encode this string again into an array. Let me know if there is any other approach to do this. thanks!

waleed
  • 464
  • 4
  • 13

5 Answers5

5

First of all your question is wrong, you want to convert int array into string.

Use this:

int[] positions = new int[] {2, 4, 3};
string result = "[" + string.Join(",", positions) + "]";

Or this:

int[] positions = new int[] {2, 4, 3};
StringBuilder stb = new StringBuilder();
stb.Append("[");
stb.Append(string.Join(",", positions));
stb.Append("]");
string result = stb.ToString();

Or if you have C#6 or higher:

int[] positions = new int[] {2, 4, 3};
string result = $"[{string.Join(",", positions)}]";

Also if you want to convert back to your int array, for example you can just write your converter:

private int[] ConvertToIntArray(string myCustomString) //myCustomString is in "[1,2,3]" format
{
    return myCustomString.Substring(1, myCustomString.Length - 2)
                         .Split(',')
                         .Select(s => int.Parse(s))
                         .ToArray();
}
SᴇM
  • 7,024
  • 3
  • 24
  • 41
  • how can i use stringBuilder? maybe like this using StringBuilder? But it didn't worked. I'm getting error namespace couldn't be found – waleed Oct 26 '17 at 08:27
  • 2
    Don't bother to use `StringBuilder` to concat just 3 strings. Just do `string result = "[" + string.Join(", ", positions) + "]";` – Matthew Watson Oct 26 '17 at 08:27
  • @MatthewWatson is right, you can just use `string result = "[" + string.Join(",", positions) + "]";` – SᴇM Oct 26 '17 at 08:29
  • it worked but Unfortunately i can't up vote your question. But thanks for the help and please up vote my question so i can up vote your answer. thanks! – waleed Oct 26 '17 at 08:34
  • thanks @SeM-ՍեՄ. But is there is any other approach to do this like any function of encode or decode? – waleed Oct 26 '17 at 08:50
  • @waleed nope, when you use your custom formats, you need to encode and decode manually. – SᴇM Oct 26 '17 at 08:51
3
"[1,2,3,4]"

is a json format of int array. Not string array.

You can use any JSON parser to do this.

I would recommend using JsonUtility which is built in Unity API. Follow this guide to understand how it works:

Serialize and Deserialize Json and Json Array in Unity

Hope this helps.

Umair M
  • 10,298
  • 6
  • 42
  • 74
1

You have asked for a string array but your example is actually just a string that happens to look how you'd declare your int array in code.

For an actual array of strings you'd do what someone else has said and use linq

string[] stringArray = positions.select(p => p.ToString());

This would give you a new array who's item data type would be string .

If you want an actual string of text that looks like what you've asked for

string stringRepresentation = $"[{string.Join(", ", positions)}]";

Or with an old fashioned string format:

string stringRepresentation = string.Format("[{0}]", string.Join(", ", positions);

But just to be clear. This is isn't in depth stuff. Some quick googling and understanding of how to use the string.Join method would have brought you this answer.

So I'm going to l leave you with a pointer towards the trim method on a string and also the split method which should give you everything you need to recreate your int array

Dave
  • 2,829
  • 3
  • 17
  • 44
1

Since you are new better use loop

int[] positions = new int[] {2, 4, 3};
string[] s = new string[positions.Length];
for (int x=0; x<positions.Length; i++)
s[x] = positions[x].ToString();
Dave
  • 36
  • 5
1

You have to do it manually.

String arrStr = "[";
for ( int i  = 0; i < arr.length() - 1; i++) {
     arrStr.join(arr[i]);
     arrStr.join(",");
 }
 arrStr.join(arr[arr.length() - 1]);
 arrStr.join("]");

Now you will have your array as you desired.