0
votes

I have a text file which have data as below

[1] [apple] [market]
[2][toy]asdv[shop]sdvdsrdt

And I have created code to extract data from [].

foreach (var line in File.ReadLines(@location))
        {
            IEnumerable<string> str;

            str = line.Split('[').Skip(1)
                .Where(x => x.Contains(']')).Select(x => x.Split(']').First());

            textBox1.Text = textBox1.Text + str.GetEnumerator() + Environment.NewLine;
        }

So now I'm stuck in deviding the data to separate text box eg ([1] [apple] [market]), I would be able to show the data is separate textbox. value 1 in textbox1, apple in textbox2, market in textbox3. and another button that will remove the data from textbox and show the second line of data in the text file. can any body help me with this

1
Is there a reason that you're trying to use that enumerator as a string? Is there more code? What exactly is the issue besides this not putting the actual text in textBox1?cory.todd
actually I'm new in this, and the code that I use is from the internet. the reason I use it is because I'm want to get only the data inside the [] and discard those that aren't. after that, I want to separate the data into different textbox.user3789007
You're on the right track but slow down with the LINQ if you just got this off this internet. Start with parsing one line (like you are doing with the split) and work from there. var words = line.Split('['); // Get all the individual words Iterate over the words array and look at it in the debugger to learn how this is working.cory.todd

1 Answers

0
votes

You can use a regular expression to break the line up into an array of strings. Then you can assign the values to each text box.

// Simulate a files content.
var fileLine = new List<string>() { "[1] [apple] [market]", "[2][toy]asdv[shop]sdvdsrdt"};

string[] str   = System.Text.RegularExpressions.Regex.Split(fileLine.FirstOrDefault(), @"\[([^]]+)\]");
TextBox1.Text = str[0];
TextBox2.Text = str[1];
TextBox3.Text = str[2];

When the user clicks the button, just re-run the Regex.Split but for the next line. If you just have the two lines, you can do fileLine.LastOrDefault Otherwise, you can create an integer to keep track of what index you are on.

var linesLoaded = new List<string[]>();
// Simulate a files content.
var fileLines = new List<string>() { "[1] [apple] [market]", "[2][toy]asdv[shop]sdvdsrdt"};
foreach (var line in fileLines)
{
    string[] str   = System.Text.RegularExpressions.Regex.Split(fileLine.FirstOrDefault(), @"\[([^]]+)\]");
    linesLoaded.Add(str);
}

When the app or form is loaded, assign the first value to your text boxes.

if (!linesLoaded.Any())
{
    return;
}

if (linesLoaded.Count < this.currentIndex)
{
    return;
}

if (!linesLoaded[this.currentIndex].Any() && linesLoaded[this.currentIndex].Count() != 3)
{
    return;
}

TextBox1.Text = linesLoaded.FirstOrDefault()[0];
TextBox2.Text = linesLoaded[0][1];
TextBox3.Text = linesLoaded[0][2];
this.currentIndex = 0; // Save in a field.

Then when the user presses a button, you just re-run the same method, but with this.currentIndex incremented.

this.currentIndex++;