0

I want to process the data block by block separately

Here is the text:

[Global]
asd
dsa
akl
ASd

[Test2]
bnmnb
hkhjk
tzutzi
Tzutzi
Tzitzi

[Test3]
5675
46546
464
564
56456
45645654
4565464

[other]
sdfsd
dsf
sdf
dsfs

And first I want the first block and process it than the second... etc..

private void textprocessing(string filename)
{
    using (StreamReader sr1 = new StreamReader(filename))
    {
        string linetemp = "";
        bool found = false;
        int index = 0;

        while ((linetemp=sr1.ReadLine())!=null)
        {
            if (found==true)
            {
                MessageBox.Show(linetemp);
                break;   
            }

            if (linetemp.Contains("["))
            {
                found = true;
            }
            else
            {
                found = false;
            }                                                             
        }                                    
    }          
}
Richard Boyce
  • 413
  • 5
  • 12
  • is that an .ini file? https://stackoverflow.com/questions/217902/reading-writing-an-ini-file – Slai Aug 02 '17 at 11:24

2 Answers2

1

You can use string.Split() to split your strings based on "[" then split that based on newlines. THen you check for the presence of "]"

void Main()
{
    string txt = @"[Global]
asd
dsa
akl
ASd

[Test2]
bnmnb
hkhjk
tzutzi
Tzutzi
Tzitzi

[Test3]
5675
46546
464
564
56456
45645654
4565464

[other]
sdfsd
dsf
sdf
dsfs";

    string[] split = txt.Split('[');
    foreach(var s in split)
    {
        var subsplits = s.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        Console.WriteLine(subsplits[0]);
        foreach(var ss in subsplits)
        {
            if(!ss.Contains("]"))
                Console.WriteLine(ss);
        }
    }
}

This outputs

asd
dsa
akl
ASd


bnmnb
hkhjk
tzutzi
Tzutzi
Tzitzi


5675
46546
464
564
56456
45645654
4565464


sdfsd
dsf
sdf
dsfs

You could add an aditional check to check if it's a blank line and ignore it.

Stuart
  • 3,949
  • 7
  • 29
  • 58
  • It is a good solution, but I want to solve it with cycle and without built in function like split, can you help me with that? – Laszlo BOZSAR Aug 02 '17 at 11:02
0

Here's one approach:

private void ReadFile()
{
    //load all  lines
    var lines = File.ReadAllLines(@"c:\temp\file.txt").ToList().;
    //remove empty lines
    lines = lines.Where(l => l.Trim().Length > 0).ToList();
    //mark indexes where sections start
    var sectionIndexes = lines
        .Where(l => l.StartsWith("[") && l.EndsWith("]"))
        .Select(l => lines.IndexOf(l)).ToList();

    //now make list of tuples. Each tuple contains start of section (Item1) and last line of section (Item2)
    var sections = Enumerable.Zip(sectionIndexes, sectionIndexes.Skip(1), (a, b) => new Tuple<int, int>(a, b-1)).ToList();

    //for each tuple (each section)
    foreach (var item in sections)
    {
        //process section name (line with raound brackets
        ProcessSection(lines[item.Item1], lines.Where(l => lines.IndexOf(l) > item.Item1 && lines.IndexOf(l) <= item.Item2));
    }
}

private void ProcessSection(string sectionName, IEnumerable<string> lines)
{
    Console.WriteLine("this is section {0} with following lines: {1}", sectionName, string.Join(", ", lines.ToArray()));
}

output of ProcessSection method would be:

this is section [Global] with following lines: asd, dsa, akl, ASd
this is section [Test2] with following lines: bnmnb, hkhjk, tzutzi, Tzutzi, Tzitzi
this is section [Test3] with following lines: 5675, 46546, 464, 564, 56456, 45645654, 4565464

This is pretty quick and dirty solution, but it would suffice if file you're reading is small.

If you have additional questions, feel free to ask.

Nino
  • 6,931
  • 2
  • 27
  • 42