I am writing a 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 am struggling past couple of days to split the read line in appropriate manner.
I am pasting a code which I have written to accomplish the same. Kindly let me know your inputs or suggest me the better way.
Thanks
UPDATE
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] =
and so on
Code
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();
}
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. – DotNetGeek Jul 19 '13 at 8:04