c# string split ignores empty values between delimiters

311 views Asked by At

Hey i already asked this but its not solved still, nothing worked , here my complete code:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Insertss
{
class Program
{
    static void Main()
    {
        ReadWrite("projektangebote.txt", "inserts.txt");
    }

    static void ReadWrite(string readName, string writeName)
    {
        StreamReader streamReader;
        StreamWriter streamWriter;
        string str;
        List<string> values = new List<string>();
        streamReader = File.OpenText(readName);
        str = streamReader.ReadLine();
        streamWriter = new StreamWriter(writeName);

        while (str != null)                                         
        {
            values.Add(str);                                   
            str = streamReader.ReadLine();               
        }

        foreach (string a in values)
        {
            int temp = 1;
            String[] temparray = a.Split(';');
            streamWriter.WriteLine("Insert into table Firma values({0},'{1}','{2}')", temp, temparray[1], temparray[4]);
            temp++;
        }
        streamReader.Close();
    }
    }
}

Ok i insert:

PR_Arbeitstitel;PR_Bereich;PR_Firma_Name;PR_Firma_Organisation;PR_Massnahme;PR_Standort;PR_ProjektTeamDaten_ProjektOrt

Überarbeitung der SAV Seite;b.i.b.;;;;PB;

But and the 2nd row ( Überarbeitung der SAV Seite;b.i.b.;;;;PB; ) i get a IndexOutOfRangeException because: Debugged

Anyone knows how i can solve this problem ? Either insert just some char or a space ..

1

There are 1 answers

4
Kamil Budziewski On

JAVA-Apllication;b.i.b;Novabig....

This is string you are splitting -> see debugger variable a, also you can see that temparray contains parts of string I presented.

it is possible you are reading wrong file (check names and directories). You can check manually which lines are read in debugger and see if it matches expected value.

Split string is working here fine, for string a it should give array of 3, because string a has only two ;'s