I am writing simple code to accept/read a input of lines from a text file, split it into my class variables, sort them and finally display the input in the ordered form. But I have been struggling for the past couple of days to split the read line in appropriate manner.
static void Main(string[] args)
{
List<MyClass> myClassList = new List<MyClass>();
string strpath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\input.txt";
if (File.Exists(strpath))
{
using (var sr = new StreamReader(strpath))
{
string[] strFirst, strSecond;
while (sr.Peek() >= 0)
{
strFirst = Regex.Split(sr.ReadLine().Replace(" ", ""), "([0-9]+)");
strFirst = Regex.Split(sr.ReadLine().Replace("|", ">>"), "([0-9]+)");
myClassList.Add(new MyClass
{
Number = strFirst[1], Name = strFirst[2], Separator = strFirst[1].Split(">>")}
});
}
}
}
myList = GetResults(myClassList);
foreach (MyClass myclass in myList)
{
Console.WriteLine(myclass[i].Number + myclass[i].Name + myclass[i].Separator);
Console.WriteLine(myclass.Name);
}
Console.ReadLine();
}
Sample input line from text file, which I want to process:
var input = "5 string one | 5.2 string two | 5.2.1 string three\r\n" +
"5 string four >> 5.6 string five >> string six\r\n" +
"1 string seven | 1.1 string eight | 1.1.1 string nine\r\n";
Output:
MyClass Number[0] = 5 Name[0] = string one Separator[0] = | Number[1] = 5.2 Name[1] = string two Separator[1] = | Number[2] = 5.2.1 Name[2] = string three Separator[2] =
char[] delim = {'|','>' }; strFirst = sr.ReadLine().Split(delim);
I getstrFirst[0] = 5 string one & strFirst[1] = 5.1 string two & strFirst[2] = string three
but now i want to further process these indexes into number and strings so that I can store them into my class variable. Hope, it makes sense. \$\endgroup\$