【发布时间】:2013-05-23 22:50:36
【问题描述】:
我正在读取的文件中有一个行列表,它们看起来像这样:
[something]:[here]
[something]:[here]
[something]:[here]
[something]:[here]
现在下面的代码基本判断列表中是否有任何东西在TextBox中,如果文本框包含一个“key”,那么key就会被替换为key的值。
string key, value, tempLine = "";
using (StringReader reader = new StringReader(list))
{
string line;
string[] split;
while ((line = reader.ReadLine()) != null)
{
// Do something with the line.
tempLine = line.Replace("[", "");
tempLine = tempLine.Replace("]", "");
split = tempLine.Split(':');
key = split[0];
value = split[1];
key = key.Replace(@"[", "");
key = key.Replace(@"]", "");
value = value.Replace(@"[", "");
value = value.Replace(@"]", "");
if (((TextBox)tabControl1.SelectedTab.Controls[0]).Text.Contains("[" + key + "]"))
{
((TextBox)tabControl1.SelectedTab.Controls[0]).Text = ((TextBox)tabControl1.SelectedTab.Controls[0]).Text.Replace(key, value);
}
}
}
现在我遇到的问题是,无论我做什么 --- 括号([ 和 ])都会不断出现!
请问,我试图摆脱括号串的方式有问题吗?如何让他们离开?
【问题讨论】:
-
你能解释一下“回来”吗,或许可以举个例子?
-
你已经删除了两次,应该够了。描述在何时何地发生了什么。
-
也许尝试第三次擦除它们?
-
你的最后两行代码本质上是
if ("[foo]".Contains("foo")) Text = "[foo]".Replace("foo", "bar");如果你把"[foo]"中的foo换成bar,那么结果显然是"[bar]"。 -
对付僵尸括号的唯一办法]就是砍掉他们的头,像这样˩