Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create dynamic string array in C# and add strings (outcome of a split method) into two separate arrays through a loop

I have a list of strings which includes strings in format: xx@yy

xx = feature name

yy = project name

Basically, I want to split these strings at @ and store the xx part in one string array and the yy part in another to do further operations.

string[] featureNames = all xx here

string[] projectNames = all yy here

I am able to split the strings using the split method (string.split('@')) in a foreach or for loop in C# but I can't store two parts separately in two different string arrays (not necessarily array but a list would also work as that can be converted to array later on).

The main problem is to determine two parts of a string after split and then appends them to string array separately.

like image 832
Indigo Avatar asked Jan 31 '26 08:01

Indigo


1 Answers

This is one simple approach:

var xx = new List<string>();
var yy = new List<string>();
foreach(var line in listOfStrings)
{
   var split = string.split('@');
   xx.Add(split[0]);
   yy.Add(split[1]);
}

The above instantiates a list of xx and and a list of yy, loops through the list of strings and for each one splits it. It then adds the results of the split to the previously instantiated lists.

like image 114
Oded Avatar answered Feb 01 '26 20:02

Oded



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!