Skip to main content
deleted 122 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

string String compare, sort, regex split

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 amhave been struggling for the 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();
    }

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] =

string compare, sort, regex split

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();
    }

String compare, sort, regex split

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] =
Post Migrated Here from stackoverflow.com (revisions)
Source Link
DotNetGeek
DotNetGeek

string compare, sort, regex split

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();
    }