我有兩个文本文本1:
100002 ステージ2 銀世界Ⅰ
100001 ステージ1 遭難Ⅰ
100004 ステージ4 出会いⅠ
100006 ステージ6 二人の約束Ⅰ
100003 ステージ3 遭難Ⅱ
100007 ステージ7 銀世界Ⅱ
100005 ステージ5 出会いⅡ文本2:
100002 第2關銀世界I
100007 第7關銀世界II
100003 第3關遇難II
100005 第5關相遇II
100006 第6關兩人的約定I
100004 第4關相遇I
100001 第1關遇難I
如文所以 两个文本都有一样的关键key和一様的字串 只不过是语言不同
而且文本内的字串排序的方式不一样我想实现读取文本2前面的key字串和文本1的key字串
如果一样 就自动把文本2后面的字串替换到文本1对应key后面的字串我只是刚刚学c#的字串 想问一下有没有例子或相关的函数可以实现这功能谢谢大家

解决方案 »

  1.   

        //句首的数字串作为key,其余去掉多余空格作为value
        struct Statement { public string key; public string value;}    class Program
        {
            static void Main(string[] args)
            {
                double x = Math.Log(0);
                int n = int.Parse(Console.ReadLine());
                System.Collections.Generic.List<Statement> listToChinesizing = new List<Statement>();
                //此处初始化listToChinesizing并导入数据
                System.Collections.Generic.Dictionary<string, string> dictDestChinese = new Dictionary<string, string>();
                //此处初始化dictDestChinese并导入数据
                for (Int32 I = 0; I < listToChinesizing.Count;I++ )
                {
                    string s;
                    if (dictDestChinese.TryGetValue(listToChinesizing[I].key, out s))
                    {
                        Statement st = new Statement();
                        st.key = listToChinesizing[I].key;
                        st.value = s;
                        listToChinesizing[I] = st;
                    }
                }
                Console.ReadKey();
            }
        }
      

  2.   

    基本上會利用  System.IO 這個命名空間下的 File 類別開啟文本二檔案,宣告一個二維陣列(File2[,]),第一個元素放Key值,第二個陣列放字串。
    然後再開啟文本一檔案,開始用 StreamReader  類別的 ReadLine 讀取每一行,我看你的 Key值都是固定6個字元長度,可以在 ReadLine 後用  SubString() 讀取左邊起算第0到第5個字元放到一個字串(strF1Key) , 然後把  strF1Key  的值和 File2[,] 陣列中的每一筆資料的第一個元素做比對,如果相同,就把 File2[,] 裡的第二個元素 用  Replace()  函數取代 strF1Key 後面的那一部分字串。大致上邏輯是這樣,您參考看看。
      

  3.   

    查重去重方法多的是baidu一堆,
    麻烦的是把你哪些个文本拆分放到固定结构里,
    文本有规律么?
      

  4.   


                string[] array1 = File.ReadAllLines("1.txt", Encoding.Default);
                string[] array2 = File.ReadAllLines("2.txt", Encoding.Default);
                var list = from x in array1
                           join y in array2
                           on x.Split('\t')[0] equals y.Split('\t')[0] into temp
                           from z in temp.DefaultIfEmpty()
                           select new
                            {
                                result = x.Split('\t')[0] + "\t" + ((z == null) ? x.Split('\t')[1] : z.Split('\t')[1])
                            };            foreach (var temp in list)
                {
                    Console.WriteLine(temp.result);
                }