-2

I have access to one string variable named Storage. I would like to save an array of strings to the Storage variable along with other variables. Is this possible to save an array variable to a string? If it is how do i get the values out of the array? See code I have below.

// This Storage variable is just for code functionality. I do not create it.
string Storage; // See above^

string Apple = "Red";
string Bannana = "Yellow";

Queue<string> myQ = new Queue<string>();
myQ.Enqueue("zero");
myQ.Enqueue("one");
myQ.Enqueue("two");
myQ.Enqueue("three");

string[] myQ_Array = myQ.ToArray();

Storage = Apple + ";" + Bannana + ";" + myQ_Array[0] + ";" + myQ_Array;
var mySplitStorage = Storage.Split(';');
Console.WriteLine("mySplitStorage[0] = " + mySplitStorage[0]);
Console.WriteLine("mySplitStorage[1] = " + mySplitStorage[1]);
Console.WriteLine("mySplitStorage[2] = " + mySplitStorage[2]);//<--This works
Console.WriteLine("mySplitStorage[3] = " + mySplitStorage[3]);//<--Cant get this to work

Console.Read();

// This is the output
//mySplitStorage[0] = Red
//mySplitStorage[1] = Yellow
//mySplitStorage[2] = zero
//mySplitStorage[3] = System.String[]  <--- How do i get the values out of the array?
Hester
  • 17
  • 1
  • 7
  • `var third = mySplitStorage[3];` and then `third[0]` or `mySplitStorage[3][0]` – Ehsan Sajjad Jan 17 '17 at 20:30
  • 1
    Is there a reason you're creating a queue to then convert it to an array, only to never use the queue again? Unless it's used later in your code and you simply didn't include that portion, why not just create the queue as an array to begin with? – MattD Jan 17 '17 at 20:37

2 Answers2

3

You can store a string as part of a string. So, if you want to store the array, you'll need to convert it to a string. myQ_Array[0] works because you are telling it to grab the string at the first position, which is 'zero'. If you want to show 'one', then you would do myQ_Array[1] since 'one' is in the second position. Keep in mind that arrays are zero indexed...so when I say first position, that is at index zero and when I say second position, that is at index one. Just putting myQ_Array doesn't work, because that is referencing the object, and not the string value. Anyhow, if you want convert the entire array into a string, you can do this:

string.Join(",", myQ_Array);

That will create a comma separated string based on your array.

Using your code, you can do:

Storage = Apple + ";" + Bannana + ";" + myQ_Array[0] + ";" + string.Join(",", myQ_Array);
Skyrider
  • 111
  • 1
  • 3
  • Thanks Skyrider I just got the code to work using the Join command. I converting the entire array to string then split it again. – Hester Jan 18 '17 at 02:31
0

String.Join and String.Split are a winning team. Split breaks strings apart into an array by a delimiter. Join brings elements of an array together joined by a delimiter.

Try this to see a comma separated list of the values of your array:

Console.WriteLine("myQ_Array = " + String.Join(",", myQ_Array));

You could also access individual elements of the array

Console.WriteLine("myQ_Array[0] = " + myQ_Array[0]);
GantTheWanderer
  • 1,255
  • 1
  • 11
  • 19