2

I am trying to split a string made of words, separated by the delimiter "-" and ",". The problem is that my program simply doesn't want to save anything in "var tokens". I already tried making "tokens" a string[], tried to use a char[] separator instead of putting "-" directly in the Split method, and tried the syntax "StringSplitOptions.RemoveEmptyEntries, but nothing works.

Here is my code:

        if (!string.IsNullOrEmpty(destin) && string.IsNullOrEmpty(depar))
        {
            try
            {
                writer.WriteLine("SearchDest");
                writer.WriteLine(destin);
                string retur = reader.ReadLine();
                Debug.WriteLine(retur);
                var tokens = retur.Split('-');
                flight.Clear();
                foreach (string s in tokens)
                {
                    Debug.WriteLine(s);
                    String[] flyelem = s.Split(',');
                    int idf = Convert.ToInt32(flyelem[0]);
                    String destf = flyelem[1];
                    String airf = flyelem[2];
                    int frees = Convert.ToInt32(flyelem[3]);
                    String datef = flyelem[4];
                    Flight b = new Flight(idf, destf, airf, frees, datef);
                    flight.Add(b);
                }

                dataGridView3.DataSource = null;
                dataGridView3.Refresh();
                dataGridView3.DataSource = flight;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

The lines

string retur = reader.ReadLine();
Debug.WriteLine(retur);

will print: -6,Moscow,Domodedovo,30,4/3/2017 12:00:00 AM-7,Moscow,Vnukovo,30,4/3/2017 12:00:00 AM-9,Moscow,Vnukovo,40,4/3/2017 12:00:00 AM

and the line "Debug.WriteLine(s);" will always print nothing, just an empty space, the program stopping when it tries to parse the string to int at int idf.

How can I fix this problem and make split to work? Thank you.

EDIT: Problem fixed. Tommy Naidich suggestion regarding using new[] {'-'} and Gunther Fox one of using StringSplitOptions.RemoveEmptyEntries as the second argument worked, and now the split works as intended. Final code for people who will encounter this problem in the future. Thank you guys.

        if (!string.IsNullOrEmpty(destin) && string.IsNullOrEmpty(depar))
        {
            try
            {
                writer.WriteLine("SearchDest");
                writer.WriteLine(destin);
                string retur = reader.ReadLine();
                Debug.WriteLine(retur);
                string[] output = retur.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                flight.Clear();
                foreach (string s in output)
                {
                    Debug.WriteLine(s);
                    string[] flyelem = s.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    int idf = Convert.ToInt32(flyelem[0]);
                    string destf = flyelem[1];
                    string airf = flyelem[2];
                    int frees = Convert.ToInt32(flyelem[3]);
                    string datef = flyelem[4];
                    Flight b = new Flight(idf, destf, airf, frees, datef);
                    flight.Add(b);
                }

                dataGridView3.DataSource = null;
                dataGridView3.Refresh();
                dataGridView3.DataSource = flight;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Artyomska
  • 1,309
  • 5
  • 26
  • 53
  • Of course it has to do everything with split, since there the program throws an exception since flyelem[0] is null because the split didn't worked. – Artyomska Apr 19 '17 at 11:46
  • It's because of the leading `-` which causes `Split` to generate an empty string in `tokens[0]`. Use the overload with `StringSplitOptions` and pass `StringSplitOptions.RemoveEmptyEntries`. – Ivan Stoev Apr 19 '17 at 11:46
  • 1
    Try using `StringSplitOptions.RemoveEmptyEntries` as the second argument in `Split`. – Foggzie Apr 19 '17 at 11:48
  • 2
    Make sure your characters are ok: Sometimes the '-' is not really a minus but another dash character (n-dash, m-dash etc).. – TaW Apr 19 '17 at 11:57

2 Answers2

1

Use the following syntax and change it to your desire.

string input = "-6,Moscow,Domodedovo,30,4/3/2017 12:00:00 AM-7,Moscow,Vnukovo,30,4/3/2017 12:00:00 AM-9,Moscow,Vnukovo,40,4/3/2017 12:00:00 AM";
string[] output = input.Split(new[] {'-', ','});

foreach(string s in output)
    Console.WriteLine(s); // Will print each one of the split words.
Tommy Naidich
  • 752
  • 1
  • 5
  • 23
  • It has to be split twice because the `-` split will give him "1 record" and the `,` split the columns so to speak – EpicKip Apr 19 '17 at 11:52
1

Your main issue lies in not checking whether s is empty or not before trying to parse to an int. Adding the additional check before conversions means the loop will properly skip the first element in the array which is blank since your string begins with -.

Also, you were using String instead of string. Please see this answer as to why that's not advised.

You can also use int.TryParse instead of Convert.ToInt32 for some extra error checking.

Working dotnetfiddle

if (!string.IsNullOrEmpty(destin) && string.IsNullOrEmpty(depar))
{
    try
    {
        writer.WriteLine("SearchDest");
        writer.WriteLine(destin);
        string retur = reader.ReadLine();
        Debug.WriteLine(retur);
        string[] tokens = retur.Split('-');
        flight.Clear();

        foreach (string s in tokens)
        {
            Debug.WriteLine(s);
            if (!string.IsNullOrEmpty(s))
            {
                string[] flyelem = s.Split(',');
                int idf;
                int frees;

                if (int.TryParse(flyelem[0], out idf) &&
                    int.TryParse(flyelem[3], out frees))
                {

                    string destf = flyelem[1];
                    string airf = flyelem[2];
                    string datef = flyelem[4];
                    Flight b = new Flight(idf, destf, airf, frees, datef);
                    flight.Add(b);
                }
            }
        }

        dataGridView3.DataSource = null;
        dataGridView3.Refresh();
        dataGridView3.DataSource = flight;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}
Community
  • 1
  • 1
Joshua Hysong
  • 1,062
  • 1
  • 18
  • 31