ここに私の問題があります。テキストファイルの内容を文字列として取得して解析しようとしています。私が欲しいのは、各単語と単語のみを含むタブです(空白、バックスペース、\n はありません...)私がしているのはLireFichier
、ファイルからテキストを含む文字列を返す関数を使用することです(正常に動作します正しく表示されているため)しかし、解析しようとすると失敗し、文字列でランダムな連結を開始し、その理由がわかりません。私が使用しているテキストファイルの内容は次のとおりです。
truc,
ohoh,
toto, tata, titi, tutu,
tete,
そして、これが私の最終的な文字列です:
;tete;;titi;;tata;;titi;;tutu;
次のようになります。
truc;ohoh;toto;tata;titi;tutu;tete;
ここに私が書いたコードがあります(使用はすべて問題ありません):
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
}