Search code examples
c#arraysconcatenation

Split a string into multiple elements


I need to split a string into multiple elements so that they can be inserted into an array. Below is an idea of what'd i'd like this to look like. The number of users is dynamic, but the string format never changes.

string Usernames = "User1, User2, User3, User4";
String[] Users = Usernames;
Console.WriteLine("First User: " + Usernames[0] + "Second User: " + Usernames[1]);
//output..
//First User: User1
//Second User: User2


Solution

  • var users = Usernames.Split(new string[] { ", " }, int.MaxValue, StringSplitOptions.None);