解析字符串C#

Wiz*_*Liz 3 c# file-io parsing

所以这是我的问题,我正在尝试将文本文件的内容作为字符串,然后解析它.我想要的是一个包含每个单词的选项卡,只包含单词(没有空白,没有退格,没有\n ......)我正在做的是使用一个函数LireFichier将包含文件中的文本的字符串发回给我(工作正常因为它显示正确)但是当我尝试解析它失败并开始对我的字符串进行随机连接时我不明白为什么.这是我正在使用的文本文件的内容:

truc,
ohoh,
toto, tata, titi, tutu,
tete,
Run Code Online (Sandbox Code Playgroud)

这是我的最后一个字符串:

;tete;;titi;;tata;;titi;;tutu;
Run Code Online (Sandbox Code Playgroud)

应该是:

truc;ohoh;toto;tata;titi;tutu;tete;
Run Code Online (Sandbox Code Playgroud)

这是我写的代码(所有使用都可以):

namespace ConsoleApplication1{

class Program
{
    static void Main(string[] args)
    {
        string chemin = "MYPATH";
        string res = LireFichier(chemin);
        Console.WriteLine("End of reading...");
        Console.WriteLine("{0}",res);// The result at this point is good
        Console.WriteLine("...starting parsing");
        res = parseString(res);
        Console.WriteLine("Chaine finale : {0}", res);//The result here is awfull
        Console.ReadLine();//pause
    }

    public static string LireFichier(string FilePath) //Read the file, send back a string with the text
    {
        StreamReader streamReader = new StreamReader(FilePath);
        string text = streamReader.ReadToEnd();
        streamReader.Close();
        return text;
    }

    public static string parseString(string phrase)//is suppsoed to parse the string
    {
        string fin="\n";
        char[] delimiterChars = { ' ','\n',',','\0'};
        string[] words = phrase.Split(delimiterChars);

        TabToString(words);//I check the content of my tab

        for(int i=0;i<words.Length;i++)
        {
            if (words[i] != null)
            {
                fin += words[i] +";";
                Console.WriteLine(fin);//help for debug
            }
        }
        return fin;
    }

    public static void TabToString(string[] montab)//display the content of my tab
    {
        foreach(string s in montab)
        {
            Console.WriteLine(s);
        }
    }
}//Fin de la class Program
}
Run Code Online (Sandbox Code Playgroud)

Hen*_*man 8

我认为你的主要问题是

  string[] words = phrase.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
Run Code Online (Sandbox Code Playgroud)