2012-04-11 185 views
3

所以这里是我的问题,我试图获取文本文件的内容作为一个字符串,然后解析它。我想要的是一个包含每个单词和只有单词的选项卡(没有空白,没有退格,没有\ 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 
} 
+2

VAR =中newstr的string.join( “;”, Regex.Matches(File.ReadAllText(@ “C:\ TEMP \ aa.txt文件”),@ “[\ W \ d] +”) 。收件人() 。选择(m => m.Value)); – 2012-04-11 09:05:57

回答

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(',', ';'); 
    } 
} 
+0

这使得这些技巧,非常感谢,我正在研究你在此刻做了什么 – WizLiz 2012-04-11 08:53:37

+0

@Wiz如果这是帮助你的答案,请点击灰色复选标记将其标记为已接受靠近投票按钮。这会给作者一些声望。这也会使他的答案相对于其他高投票率但不完整的答案更明显,因此更有可能其他人也会给他一些代表。 – Rawling 2012-04-11 09:00:27

+0

更新了一些优化的答案:) – StaWho 2012-04-11 09:19:29

8

我认为你的主要问题是

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

+1亨克,打了20秒;-) – Bridge 2012-04-11 08:41:26

+0

其实,这几乎发挥了诀窍,它克服了双重问题;;在最后一个字符串中,但仍然存在错误,如txt文件中缺少一些单词: – WizLiz 2012-04-11 08:43:58

+1

@WizardLizard请参阅我的或StaWho的答案以找到缺失的单词问题。 – Rawling 2012-04-11 08:50:31

2

你可以尝试使用字符串分割选项删除你空的条目:

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

参见文档here

1

你的主要问题是,你是在\n分裂,但换行符从文件中读取是\r\n

您输出的字符串确实包含您的所有项目,但剩下的\r个字符会导致稍后的“行”在控制台上覆盖较早的“行”。

\r是“返回到开始行”指令;而不\n“移动到下一行”指令从第1行的话正在由那些在第2行覆盖,然后线3和线4)

除了分割上\r以及\n,你需要检查的字符串不是空或空将其添加到您的输出(或者最好使用StringSplitOptions.RemoveEmptyEntries如其他人所说的)前。

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