-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];
8
  • 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? Commented Mar 23, 2018 at 15:43
  • 1
    @theMayer Similar method, different language Commented Mar 23, 2018 at 15:45
  • 4
    @theMayer Why did you mark as a duplicate of an javascript question? Commented Mar 23, 2018 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. Commented Mar 23, 2018 at 15:46
  • if only that were true Commented Mar 23, 2018 at 15:47

2 Answers 2

2

Use .Split()

string[] arr1 = Test.Split(',');
Sign up to request clarification or add additional context in comments.

3 Comments

So Quick answer thank you!
What if i want to convert in int array? int[] arr2 = Convert.ToInt32(TEst.Split(',')); I get error cant convert from int[] to int32
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.
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(',');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.