【问题标题】:Replacing text in a file with datatable values用数据表值替换文件中的文本
【发布时间】:2012-02-02 11:51:36
【问题描述】:

我们有一个带有文本的示例文本文件:

上帝为爱他的人准备的东西

我们将文本读入数据表并分配一些值,如下所示:

 The            1
----------
 things         2
----------
 God            3
----------
 has            4
----------
 prepared       5
----------
 for            6
----------
 those          7
----------
 who            8
----------
 love           9
----------
 him            10
----------

我们正在尝试用这些对应的数字替换输入文件中的文本。 可能吗?如果可能,我们该怎么做?

编辑2: 我们像这样编辑了我们的代码:

:

无效替换() {

        string s1, s2;            
        StreamReader streamReader;
        streamReader = File.OpenText("C:\\text.txt");
        StreamWriter streamWriter = File.CreateText("C:\\sample1.txt");
        int x = st.Rows.Count;
        int i1 = 0;                                       
            // Now, read the entire file into a string
            while ((line = streamReader.ReadLine()) != null)
            {
                for (int i = 0; i < x; i++)
                {
                s1 = Convert.ToString(st.Rows[i]["Word"]);
                s2 = Convert.ToString(st.Rows[i]["Binary"]);
                s2+="000";
                char[] delimiterChars = { ' ', '\t' };
                string[] words = line.Split(delimiterChars);

                    // Write the modification into the same file 
                    string ab = words[i1]; // exception occurs here
                   // Console.WriteLine(ab);
                    streamWriter.Write(ab.Replace(s1, s2));
                    i1++;                                       
                }                
            }
        streamReader.Close();
        streamWriter.Close();
    }

但是我们得到了一个“数组索引越界”异常。我们无法找到问题所在。 提前致谢

【问题讨论】:

  • 如果单词重复,是否需要使用相同的值?还是只数单词?
  • 是的。重复的单词应该具有相同的值。
  • 不。不区分大小写
  • 请发布异常,包括堆栈跟踪

标签: c# file stream


【解决方案1】:

这里有一些代码可以帮助你开始,它还没有经过广泛的测试:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            File.WriteAllText("sample1.txt", "The things God has prepared for those who love him the");

            string text = File.ReadAllText("sample1.txt").ToLower();
            var words = text
                .Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .Distinct()
                .OrderByDescending(word => word.Length);

            var values = new Dictionary<string, int>();
            for (int i = 0; i < words.Count(); i++)
            {
                values.Add(words.ElementAt(i), i + 1);
            }
            foreach (var kvp in values)
            {
                text = text.Replace(kvp.Key, kvp.Value.ToString());
            }
            File.WriteAllText("sample1.txt", text);

            Console.WriteLine("Press ENTER to exit");
            Console.ReadLine();
        }
    }
}

它创建一个测试文本文件,读取它,将其转换为小写,为不同的单词制作标识符,并根据这些标识符替换文本。长词在短词之前被替换,以提供一些不好的替换预防。

更新:我刚刚注意到问题已更新,不再可以选择在一个字符串中读取整个文件.. sigh.. 所以我的回答只适用当你一口气读完所有文本时,也许你可以在每个单词读写时重复使用其中的一部分。

【讨论】:

  • Tnx 寻求帮助。我们编辑了代码,但是我们得到了数组索引越界异常。我们更新了问题。
猜你喜欢
  • 2016-03-20
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
相关资源
最近更新 更多