【发布时间】: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();
}
但是我们得到了一个“数组索引越界”异常。我们无法找到问题所在。 提前致谢
【问题讨论】:
-
如果单词重复,是否需要使用相同的值?还是只数单词?
-
是的。重复的单词应该具有相同的值。
-
不。不区分大小写
-
请发布异常,包括堆栈跟踪