1

Lets say, Here is an array of strings in c#:

string[] array = new string[] { "one", "two", "three" };

Is there any way to convert this string array into a string like this:

 "one,two,three"

And after converting into this string, how will I get back the previous array of strings, I mean how will I convert the string into an array of strings again?

string[] array = new string[] { "one", "two", "three" };
1
  • 3
    use string.join(",", array) to join the string and use joinedstring.split(',') to get the array back Commented Sep 29, 2015 at 6:26

3 Answers 3

3

Try like this

join

var str = string.Join(",", array);

array

var strArr = str.Split(',');

DOTNETFIDDLE

Sign up to request clarification or add additional context in comments.

4 Comments

how will I display the string in the console? @Anik Islam Abhi
use a for loop or foreach loop to display in console for strArr
I meant how to display the converted string to console? using Console.Writeline(str); is not working @Anik Islam Abhi
@xls i have updated my answer. check the fiddle link
2

Your Asnwer is Join and Split will help you to do this

Join

The string.Join method combines many strings into one. It receives two arguments: an array or IEnumerable and a separator string. It places the separator between every element of the collection in the returned string.

string.Join(",", array)

Split

Often strings have delimiter characters in their data. Delimiters include "," the comma and "\t" tab characters.

string[] words = JoinedString.Split(',');

Comments

2

Given your array of strings:

string[] array = new string[] { "one", "two", "three" };

You can join it like this (there are several other ways, but this is one of the simpler ones)

var str = string.Join(",", array);

see msdn and dotnetpearls for further information on this method which also has some interestiong overoads.

Then you can turn it back to an array using the split method ob your joined string like so:

var array2 = str.Split(',');

Also, see msdn or dotnetpearls for deeper knowledge on this method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.