-3

I need to pass a string with type A,B,C,D,E(Separated with comma) in each string[] value.

String Test ="A,B,C,D,E";
string[] arr1 = new string[5];
daddycool
  • 39
  • 1
  • 6
  • Do you want each item in `arr1` to be `A,B,C,D,E`, or do you want arr1 to be `A,B,C,D,E` separated in items of the array? – Camilo Terevinto Mar 23 '18 at 15:43
  • 1
    @theMayer Similar method, different language – Jack Mar 23 '18 at 15:45
  • 4
    @theMayer Why did you mark as a duplicate of an javascript question? – Camilo Terevinto Mar 23 '18 at 15:45
  • Well, the syntax is the same, the purpose is the same, and a reasonable person should be able to make an inference as to the behavior. – theMayer Mar 23 '18 at 15:46
  • if only that were true – Ňɏssa Pøngjǣrdenlarp Mar 23 '18 at 15:47
  • @theMayer You are assuming too much. – Ryan Wilson Mar 23 '18 at 15:58
  • @Ryan Wilson - no, I don’t think so. This is not a site for folks who haven’t even done a modicum of research to show up and create spam. So an assumption of “a reasonable person” is indeed appropriate. – theMayer Mar 23 '18 at 16:41
  • @theMayer While I agree with you about the "not a site for folks who haven't done a modicum of research", I still don't agree with marking as a duplicate when the duplicate is in another programming language. I agree that the syntax is similar, but there are some differences between C# and javascript when it comes to the Split() method, as C# has 6 overloaded versions of the method. Also, my original comment was in jest. – Ryan Wilson Mar 23 '18 at 17:44

2 Answers2

2

Use .Split()

string[] arr1 = Test.Split(',');
Jack
  • 886
  • 7
  • 27
  • So Quick answer thank you! – daddycool Mar 23 '18 at 15:50
  • What if i want to convert in int array? int[] arr2 = Convert.ToInt32(TEst.Split(',')); I get error cant convert from int[] to int32 – daddycool Mar 23 '18 at 15:53
  • 1
    Use linq, int[] arr2 = Test.Split(',').Select(x => System.Convert.ToInt32(x)).ToArray(); of course this won't work since you have letters and not integer numbers represented by string values. – Ryan Wilson Mar 23 '18 at 15:54
2

If you want to insert this string into a string[] with size 5:

string[] arr1 = Enumerable.Repeat(Test, 5).ToArray();

of course the classic way is to use a for-loop:

string[] arr1 = new string[5];
for(int i = 0; i < arr1.Length; i++)
    arr1[i] = "A,B,C,D,E";

If you instead want to create a string[] from the string:

string[] arr1 = "A,B,C,D,E".Split(',');
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939