So, here is my problem, I am trying to get the contents of a text file as a string, and then parse it. What I want is a tab containing every word and only words (no spaces, no backspace, no \ n ...). I use a function LireFichierthat returns me a string containing the text from the file (works fine because it displays correctly), but when I try to parse it and start doing random concatenation on my line, and I don’t understand why. Here is the contents of the text file that I am using:
truc,
ohoh,
toto, tata, titi, tutu,
tete,
and here is my last line:
which should be:
truc;ohoh;toto;tata;titi;tutu;tete;
Here is the code I wrote (all are used in order):
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);
Console.WriteLine("...starting parsing");
res = parseString(res);
Console.WriteLine("Chaine finale : {0}", res);
Console.ReadLine();
}
public static string LireFichier(string FilePath)
{
StreamReader streamReader = new StreamReader(FilePath);
string text = streamReader.ReadToEnd();
streamReader.Close();
return text;
}
public static string parseString(string phrase)
{
string fin="\n";
char[] delimiterChars = { ' ','\n',',','\0'};
string[] words = phrase.Split(delimiterChars);
TabToString(words);
for(int i=0;i<words.Length;i++)
{
if (words[i] != null)
{
fin += words[i] +";";
Console.WriteLine(fin);
}
}
return fin;
}
public static void TabToString(string[] montab)
{
foreach(string s in montab)
{
Console.WriteLine(s);
}
}
}
}
source
share