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?

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
Josh YG
  • 1
  • 1
  • 3
    The one and only idea here is to debug your code step by step. – Leo Chapiro Jul 29 '16 at 12:00
  • Try to remove the "assignmentSplit" parameter from the List constructor. My guess is that you constrain the list to a lesser quantity of items than the expected. – Mario Vernari Jul 29 '16 at 12:07
  • What is the desired output? It is unclear to me exactly what you are trying to do. – Crowcoder Jul 29 '16 at 12:09
  • @Crowcoder Its not for me. Its just `string[] { "In-Class Test", "02/07/2014", "In-Class Test", "21/04/2013" }`. – C4d Jul 29 '16 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 – Josh YG Jul 29 '16 at 12:13
  • Replace `assignment.Split('\t')` with `Regex.Replace(assignment, Environment.NewLine, "");` – C4d Jul 29 '16 at 12:18

4 Answers4

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?

MasterXD
  • 804
  • 1
  • 11
  • 18
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".

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);
    }
}
C4d
  • 3,183
  • 4
  • 29
  • 50
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]);
    }
}
Crowcoder
  • 11,250
  • 3
  • 36
  • 45