【问题标题】:Brackets keep sneaking back in! Why? [closed]括号继续偷偷回来!为什么? [关闭]
【发布时间】: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]"
  • 对付僵尸括号的唯一办法]就是砍掉他们的头,像这样˩

标签: c# .net winforms io


【解决方案1】:

似乎您正在将由 [key] 组成的占位符搜索到您的文本框中,但是当替换时,该值仅替换键,保持 [] 不变。

你必须用这个替换你的代码...

if (((TextBox)tabControl1.SelectedTab.Controls[0]).Text.Contains("[" + key + "]"))
{
    ((TextBox)tabControl1.SelectedTab.Controls[0]).Text = ((TextBox)tabControl1.SelectedTab.Controls[0]).Text.Replace("[" + key + "]", value);
}

并且不要替换两次 [] 字符。没必要。

使用您的代码作为基线,生成的代码必须是:

string key, value, tempLine = "";

using (StringReader reader = new StringReader(list))
{
    string line;
    string[] split;
    while ((line = reader.ReadLine()) != null)
    {
        // Do something with the line.
        split = line.Split(':');
        key = split[0];
        value = split[1].Replace("[", "").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);
        }
    }
}

【讨论】:

  • 只需要在值上做Replace。线路和钥匙都不需要这样做。
  • 抱歉,@HuorSwords,这根本不起作用。
  • 我刚看到更新,正在尝试更新。
  • 完美!我刚刚尝试了更新的版本,效果很好。我很高兴 :-) 谢谢!
【解决方案2】:

或许可以试试:

((TextBox)tabControl1.SelectedTab.Controls[0]).Text = ((TextBox)tabControl1.SelectedTab.Controls[0]).Text.Replace("[" + key + "]", value);

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    相关资源
    最近更新 更多