3

ここに私の問題があります。テキストファイルの内容を文字列として取得して解析しようとしています。私が欲しいのは、各単語と単語のみを含むタブです(空白、バックスペース、\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
}
4

5 に答える 5

8

あなたの主な問題は

  string[] words = phrase.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
于 2012-04-11T08:38:21.490 に答える
2

文字列分割オプションを使用して、空のエントリを削除してみてください。

string[] words = phrase.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);

こちらのドキュメントを参照してください。

于 2012-04-11T08:38:54.580 に答える
1

主な問題は、で分割していることですが\n、ファイルから読み取られた改行は\r\nです。

出力文字列にはすべての項目が含まれていますが、文字列に\r残っている文字により、後の「行」がコンソールの前の「行」を上書きします。

\rこれは「行の先頭に戻る」命令です。\n「次の行に移動」命令がないと、1行目の単語が2行目、3行目および4行目の単語に上書きされます。)

分割する\rだけでなく、文字列を出力に追加する前に、文字列がnullまたは空\nでないことを確認する必要があります(または、できれば、他の人が述べているように使用します)。StringSplitOptions.RemoveEmptyEntries

于 2012-04-11T08:46:09.727 に答える
1

これを試して:

class Program
    {
        static void Main(string[] args)
        {
            var inString = LireFichier(@"C:\temp\file.txt");
            Console.WriteLine(ParseString(inString));
            Console.ReadKey();
        }

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

        public static string ParseString(string input)
        {
            input = input.Replace(Environment.NewLine,string.Empty);
            input = input.Replace(" ", string.Empty);
            string[] chunks = input.Split(',');
            StringBuilder sb = new StringBuilder();
            foreach (string s in chunks)
            {
                sb.Append(s);
                sb.Append(";");
            }
            return sb.ToString(0, sb.ToString().Length - 1);
        }
    }

またはこれ:

public static string ParseFile(string FilePath)
{
    using (var streamReader = new StreamReader(FilePath))
    {
        return streamReader.ReadToEnd().Replace(Environment.NewLine, string.Empty).Replace(" ", string.Empty).Replace(',', ';');
    }
}
于 2012-04-11T08:42:03.083 に答える
0
string ParseString(string filename) {
    return string.Join(";", System.IO.File.ReadAllLines(filename).Where(x => x.Length > 0).Select(x => string.Join(";", x.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(y => y.Trim()))).Select(z => z.Trim())) + ";";
}
于 2012-04-11T09:49:56.833 に答える