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

70,510

Solution 1

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.

Solution 2

How about the following:

List<String> xx = new List<String>();
List<String> yy = new List<String>();
var strings = yourstring.Split('@');

xx.Add(strings.First());
yy.Add(strings.Last());

Solution 3

You can do something like this:

var splits = input.Select(v => v.Split('@'));

var features = splits.Select(s => s[0]).ToList();
var projects = splits.Select(s => s[1]).ToList();

If you don't mind slightly more code but better performance and less pressure on garbage collector then:

var features = new List<string>();
var projects = new List<string>();

foreach (var split in input.Select(v => v.Split('@')))
{
    features.Add(split[0]);
    projects.Add(split[1]);
}

But overall I'd suggest to create class and parse your input (more C#-style approach):

public class ProjectFeature
{
    public readonly string Project;
    public readonly string Feature;

    public ProjectFeature(string project, string feature)
    {
        this.Project = project;
        this.Feature = feature;
    }

    public static IEnumerable<ProjectFeature> ParseList(IEnumerable<string> input)
    {
        return input.Select(v =>
        {
            var split = v.Split('@');
            return new ProjectFeature(split[1], split[0]);
        }
    }
}

and use it later (just an example of possible usage):

var projectFeatures = ProjectFeature.ParseList(File.ReadAllLines(@"c:\features.txt")).ToList();
var features = projectFeatures.Select(f => f.Feature).ToList();
var projects = projectFeatures.Select(f => f.Project).ToList();
// ??? etc.

Solution 4

var featureNames = new List<string>();
var productNames = new List<string>();
foreach (var productFeature in productFeatures)
{
  var parts = productFeature.Split('@');

  featureNames.Add(parts[0]);
  productNames.Add(parts[1]);
}

Solution 5

How about

List<string> lst = ... // your list containging xx@yy

List<string> _featureNames = new List<string>();

List<string> _projectNames = new List<string>();

lst.ForEach(x => 
{
    string[] str = x.Split('@');
    _featureNames.Add(str[0]);
    _projectNames.Add(str[1]);
}

string[] featureNames = _featureNames.ToArray();

string[] projectNames = _projectNames.ToArray();
Share:
70,510
Indigo
Author by

Indigo

Updated on July 09, 2022

Comments

  • Indigo
    Indigo almost 2 years

    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.

  • Oded
    Oded over 11 years
    Almost, though there is a list of strings to split, not just one.
  • Indigo
    Indigo over 11 years
    Yes, but I need to use xx and yy in different situations later on. I am not really sure how all this works so try is to keep it as simple as possible to avoid complex coding. Thanks a lot. By all these ideas, I am learning many new things.
  • CodeCaster
    CodeCaster over 11 years
    @Chetan you can always iterate over the Keys property of the dictionary to retreive all keys (which would in fact contain the same as your featureNames array). Besides, with a dictionary you only have to pass one variable to other functions instead of both arrays.
  • Indigo
    Indigo over 11 years
    Perfect, this is exactly what I am doing here. I have a treeView in my windows form application. At the end, I get checked nodes as this xx@yy name. I am writing a separate class where I can give this list as input and then use the feature and project names to retrieve required data from the database and then add them to XML or Excel or even to a Text file, whatever format the end user will select for output. Thank you so much. I guess it will take some time to learn these tricks but I am also motivated enough now.
  • Indigo
    Indigo over 11 years
    Yes, correct, got it. Reviewing more information about Dictionary. Thanks again. It is really helpful.