0

After reading a text file, I pass the information through a method that splits the string to get the required information for each field. I have gotten to the point where i split the string based on new lines, but it is beyond me why when I display the subsequent array (or list it's converted to), it only shows the first half of the first split string.

An example of how the input string looks:

ASSIGNMENT    
In-Class Test    02/07/2014     
In-Class Test    21/04/2013

Find my code below (the numLines variable was to simply see if it was being split into the correct number of lines as it should)

private void assignmentfinder(string brief, string id)
{   
    string searchcrit = "ASSIGNMENT";
    string assignment = brief.Substring(brief.IndexOf(searchcrit) + searchcrit.Length);
    string[] assignmentsplit;
    assignmentsplit = assignment.Split('\t');
    List<string> Assign = new List<string>(assignmentsplit);
    listBox2.DataSource = Assign;
    int numLines = assignment.Split('\n').Length;
    richTextBox1.Lines=(assignmentsplit);
}

The output I get is:

In-Class Test     
02/07/2014

Whereas it should show the second string split as well. Any ideas?

6
  • 3
    The one and only idea here is to debug your code step by step. Commented Jul 29, 2016 at 12:00
  • Try to remove the "assignmentSplit" parameter from the List<string> constructor. My guess is that you constrain the list to a lesser quantity of items than the expected. Commented Jul 29, 2016 at 12:07
  • What is the desired output? It is unclear to me exactly what you are trying to do. Commented Jul 29, 2016 at 12:09
  • @Crowcoder Its not for me. Its just string[] { "In-Class Test", "02/07/2014", "In-Class Test", "21/04/2013" }. Commented Jul 29, 2016 at 12:11
  • @Crowcoder I want to have each line of input string separated so i can store the type of test and the date to be pulled into a text box Commented Jul 29, 2016 at 12:13

4 Answers 4

0

The problem is in the value of "brief" variable. Make sure the value you are putting in "brief" variable, actually conains "\n" and "\t".

Sign up to request clarification or add additional context in comments.

Comments

0

You could use regex like this:

private void assignmentfinder(string brief, string id)
{
    Regex rgxLines = new Regex(@"^(.*?)[ \t]+([0-9]{2}\/[0-9]{2}\/[0-9]{4})", RegexOptions.Multiline);
    MatchCollection mLines = rgxLines.Matches(brief);

    foreach (Match match in mLines)
    {
        richTextBox1.Text += String.Format("Test: {0}{1}Date: {2}{1}{1}", 
                                            match.Groups[1].Value, 
                                            Environment.NewLine, 
                                            match.Groups[2].Value);
    }
}

Comments

0

Your mistake is that you split for each tab (\t). Split for the new line as you say:

private void assignmentfinder(string brief, string id)
{
    string searchcrit = "ASSIGNMENT";
    string assignment = brief.Substring(brief.IndexOf(searchcrit) + searchcrit.Length);

    string[] assignmentSplit = assignment.Split('\n'); // splitting at new line

    richTextBox1.Lines = assignmentSplit;

    listBox2.DataSource = assignmentSplit.ToList();
}

I hope this helps.

EDIT: Just fixed a huge mistake

Question: Is it on purpose that the string id is never used?

Comments

0

You could use Linq to split on new line, then on tab, skipping the first line then aggregate into a list of string:

string brief = @"ASSIGNMENT
In-Class Test   02/07/2014
In-Class Test   21/04/2013";

List<string> theLines = new List<string>();     
var lines = brief.Split('\n').Skip(1).Select (b => b.Split('\t'));
foreach (var line in lines)
{
    for (int i = 0; i < line.Length; i++)
    {
        theLines.Add(line[i]);
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.