将竖的形式的数字转化为以逗号隔开的形式,要求只筛选出不重复的形式。

class Program
    {
        static void Main(string[] args)
        {
            string rpath;     //读取文件的路径
            string wpath;     //写文件的路径
            string readstring;  //读取的文本
            string[] stringarray ;  //将文本转换为数组
            string laststring;     //最后需要的文本
            
            rpath = "D:\\a.txt";
            wpath = "d:\\b.txt";
            readstring = ReaderText(rpath);
            stringarray = readstring.Split(',');
            laststring = DeleteSame(stringarray);
            WriteText(wpath,laststring);
            Console.WriteLine("Translate Successfully");
            Console.ReadKey();
        }
        //从文件中读取数据
        public static string ReaderText(string path)
        {
            string s = "";
            try
            {
                using (StreamReader sr = new StreamReader(path))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        //Console.WriteLine(line);
                        s = s + line + ','; ;

                    }
                    s = s.Remove(s.Length - 1);//将最后一个逗号移除
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read");
                Console.WriteLine(e.Message);
            }
            return s;
        }

        //移除相同的数据
        public static string DeleteSame(string[] stringarry)
        {
            List<string> liststring = new List<string>();
            foreach (string eachstring in stringarry)
            {
                if (!liststring.Contains(eachstring))
                {
                    liststring.Add(eachstring);
                }
            }
            string laststring = "";
            foreach (string eachstring in liststring)
            {
                laststring = laststring + eachstring + ',';
            }
            return laststring.Remove(laststring.Length - 1);
        }

        //写入文件
        public static void WriteText(string wpath,string data)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(wpath))
                {
                    sw.Write(data);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }

 

posted on 2013-10-29 14:19  Kelvin Xu  阅读(212)  评论(0编辑  收藏  举报