How do I store an array in a string? [duplicate]

4

Example:

string x;
string[] y = {"5", "0","0","5","5"};
x = "50055"

I want to put everything together into one variable. I will store in a non-print variable.

    
asked by anonymous 18.11.2017 / 22:18

2 answers

7

Only use Join

    string[] y = {"5", "0","0","5","5"};
    string x = string.Join("",y);
    Console.WriteLine(x);

See in .NetFiddle: link

Documentation: link

    
18.11.2017 / 22:23
2

You can use String.Join when you need a separator. So:

string[] nomes = {"Vinicius", "Rovann", "Anderson"};
return string.Join(";", nomes);
// Saída: "Vinicius;Rovann;Anderson"    

If you want to concatenate an array of strings in a single string, you can use String.Concat . See:

string[] arr = {"1", "2", "3"};
return string.Concat(arr);
// Saída: "123"
    
19.11.2017 / 00:43