これは私の問題です。テキストファイルの内容を文字列として取得して解析しようとしています。私がしたいのは、各単語と単語だけを含むタブです(空白、バックスペース、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
}
VAR NewStrによってすでに= String.Join( ";"、 Regex.Matches(File.ReadAllText(@ "C:\一時\のaa.txt")、@ "[\ W \ D] +") .Cast() .Select(m => m.Value)); –