所以这里是我的问题,我试图获取文本文件的内容作为一个字符串,然后解析它。我想要的是一个包含每个单词和只有单词的选项卡(没有空白,没有退格,没有\ n ...)我在做的是使用函数LireFichier
,将包含文件的字符串发回给我(作品罚款,因为它显示正确),但当我试图解析它失败,并开始做我的字符串随机连接,我不明白为什么。 这里是文本文件我使用的内容:这应该是解析字符串C#
;tete;;titi;;tata;;titi;;tutu;
:
truc;ohoh;toto;tata;titi;tutu;tete;
这里是我的代码
truc,
ohoh,
toto, tata, titi, tutu,
tete,
这是我的最后一个字符串写道(全部使用都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:\ TEMP \ aa.txt文件”),@ “[\ W \ d] +”) 。收件人() 。选择(m => m.Value)); –
2012-04-11 09:05:57