SQLserving
SQLserving

Reputation: 400

separating strings by some text within listbox

I have a ListBox where table names are written like this:

Staging_Section_01_2019_03_19_01  
Staging_Section_01_2019_03_20_01  
Staging_Section_23_2019_03_21_01  
Staging_Section_52_2019_03_23_01  
Staging_Section_52_2019_03_24_01  

What I am trying to do is to separate them by Section Number, so I want all Section_01 in one List object and Section_23 in another List object, so on and so forth. The dynamic nature is whats making it difficult for me.

So far, I have the following:

foreach (var it in uploadBox.Items)
{
    if (it.ToString().Contains("Section"))
    {
        section = it.ToString().Substring(0, 18);
        found = it.ToString().IndexOf("_");
        section = section.Substring(found + 1);
        sectionNum = section.Substring(8, 2);
    }
}

I have gotten the sectionNum which would just be the number and section, which is the string like Section_01.

Any idea on how to approach this?

The expected output would be something like this:

List 1

Staging_Section_01_2019_03_19_01  
Staging_Section_01_2019_03_20_01  

List 2

Staging_Section_23_2019_03_21_01  

List 3

Staging_Section_52_2019_03_23_01  
Staging_Section_52_2019_03_24_01  

Upvotes: 0

Views: 64

Answers (3)

Xiaoy312
Xiaoy312

Reputation: 14477

It is best to regex for this kind of task:

uploadBox.Items
    .GroupBy(x => Regex.Match(x.ToString(), @"^\w+_Section_(?<section>\d+)").Groups["section"].Value)

Upvotes: 0

Pedro
Pedro

Reputation: 2310

you could do something like this:

var sections = new Dictionary<string, List<string>>();

foreach(var it in uploadBox.Items)
{
    var item = it.ToString();

    if(item.Contains("Section"))
    {

        var section = GetSection(item);

        if(!sections.ContainsKey(section))
        {
            sections.Add(section, new List<string>());            
        } 

        sections[section].Add(item);
    }    
}

private string GetSection(string item)
{
    var split = item.Split("_");
    return $"{split[1]}_{split[2]}";    
}

Upvotes: 0

Ingenioushax
Ingenioushax

Reputation: 718

I would use a Dictionary<string, List<string>> for this. Each 'section' that is parsed would be a key, and the remaining portion would the the value.

Dictionary<string, List<string>> myDict = new Dictionary<string, List<string>>();
foreach (var it in uploadBox.Items)
{
    if (it.ToString().Contains("Section"))
    {
        section = it.ToString().Substring(0, 18);
        found = it.ToString().IndexOf("_");
        section = section.Substring(found + 1);
        sectionNum = section.Substring(8, 2);

        if(!myDict.ContainsKey(sectionNum))
        {
            myDict.Add(sectionNum, new List<string> { someOtherValue });
        }
        else
        {
            myDict[sectionNum].Add(someOtherValue);
        }
    }
}

Unless I have completely misinterpreted your question, I think this is a potential solution to your dynamic objects.

Upvotes: 1

Related Questions