2012-04-11 33 views
3

これは私の問題です。テキストファイルの内容を文字列として取得して解析しようとしています。私がしたいのは、各単語と単語だけを含むタブです(空白、バックスペース、no \ n ...)。私がやっていることは、ファイルからテキストを含む文字列を返す関数LireFichierを使用しています正常に表示されているので問題はありません)。しかし、解析しようとすると失敗し、文字列にランダム連結を開始します。理由はありません。ここ は、私が使用しているテキストファイルの内容です:あるべき文字列を解析するC#

;tete;;titi;;tata;;titi;;tutu; 

truc, 
ohoh, 
toto, tata, titi, tutu, 
tete, 

、ここでは私の最後の文字列です。ここ

truc;ohoh;toto;tata;titi;tutu;tete; 

は、コードIです書きました(使用はすべてOKです):

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 
} 
+2

VAR NewStrによってすでに= String.Join( ";"、 Regex.Matches(File.ReadAllText(@ "C:\一時\のaa.txt")、@ "[\ W \ D] +") .Cast () .Select(m => m.Value)); –

答えて

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); 
     } 
    } 

またはこの:あなたの主な問題は、あなたが\nに分割されているということですが、改行は、ファイルから読み込ん

public static string ParseFile(string FilePath) 
{ 
    using (var streamReader = new StreamReader(FilePath)) 
    { 
     return streamReader.ReadToEnd().Replace(Environment.NewLine, string.Empty).Replace(" ", string.Empty).Replace(',', ';'); 
    } 
} 
+0

お返事ありがとうございました。ありがとうございました。あなたが今行ったことを勉強しています。 – WizLiz

+0

@Wizこれがあなたを助けた答えであれば、灰色のチェックマーク投票ボタンの近くに。これは著者にいくつかの評判を与えるでしょう。また、彼の答えは他の投票者よりも見やすく、不満足な回答になるので、他の人も彼に何人かの担当者を与える可能性が高くなります。 – Rawling

+0

はいくつかの最適化で答えを更新しました:) – StaWho

8

私はあなたの主な問題は、あなたがあなたのために空のエントリを削除する文字列分割オプションを使用して試みることができる

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

+1ヘンク、20秒で私を打ち負かしてください;-) – Bridge

+0

実際には、それはほとんどそのトリックを作った、それはダブルとの問題を克服;最終的な文字列にはまだtxtファイルからいくつかの単語が欠けているような間違いがあります: – WizLiz

+1

@WizardLizard欠落単語の問題については、mineまたはStaWhoの回答を参照してください。 – Rawling

2

だと思う:

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

は、ドキュメントhereを参照してください。

1

\r\nです。

出力文字列にすべてのアイテムが含まれていますが、\r文字が残っていると、後の「行」がコンソールの以前の「行」を上書きします。

\rは、\n「次の行に移動」命令を使用しないと、1行目のワードが2行目、次に3行目および4行目のワードで上書きされます。

と同様に\rなど\nに分割、あなたは文字列をチェックする必要があります(他の人が言及したように好ましく、StringSplitOptions.RemoveEmptyEntriesを使用するか、または)あなたの出力に追加する前に、または空 nullではありません。

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())) + ";"; 
}