如何在C#中拆分文本文件中的数据

时间:2010-04-22 11:10:47

标签: c#-3.0

。这就是我的数据外观:name:abcsurname:abctel:1234我希望它看起来像:name:abc和surname:abc应该在下一行。

        public void SeparateData()
        {
            //read file
            StreamReader sr = new StreamReader("myTextFile.txt");
            //string to hold line
            string myline;
            myline = sr.ReadLine();
            while ((myline = sr.ReadLine()) != null)
            {
                string[] lines = Regex.Split(myline, " ");
                foreach (string s in lines)
                {
                    using (StreamWriter sw = new StreamWriter("myTextFile.txt"))
                        sw.WriteLine(lines);
                }

            }
        }

1 个答案:

答案 0 :(得分:0)

好吧,你不需要正则表达式在空格上进行字符串拆分,但我不会让你想要你想要的任何一种方式。我假设“abc”代表不同长度的实际值?

我认为你只需要拿出你的正则表格书并拆分字符串并重写它。

e.g。 '(name:)(\ w *?)(姓:)(\ w *?)(tel:)(\ d *)'然后只需使用捕获组重新组装并重写该行。

我认为你所拥有的将会给你:

名:
abcsurname:
abctel:
1234