【问题标题】:Replace returns 2 of the same value?替换返回 2 个相同的值?
【发布时间】:2011-10-15 10:11:15
【问题描述】:

我有一个问题,为什么会这样。首先,我将解释发生了什么。我在RichTextBox 中找到一行并取Split 值并将其替换为相同的值但有小数限制。这是我的文件的样子:

J6   INT-00113G  227.905  174.994    180   SOIC8
J3   INT-00113G  227.905  203.244    180   SOIC8
U13  EXCLUDES    242.210  181.294    180   QFP128

但由于某种原因,当我尝试替换并将其输出回来时,我得到了这个... (数字在第 3 列和第 4 列中都出现了两次)

J6   INT-00113G  227.91227.91   174.99174.99     180   SOIC8
J3   INT-00113G  227.91227.91   203.24203.24     180   SOIC8
U13  EXCLUDES    242.21242.21   181.29181.29     180   QFP128

这是我的代码...这样做有什么错误?

string[] myLines = placementTwoRichTextBox.Text.Split('\n');
foreach (string line in myLines)
{
    // Matches the entire line.
    Match theMatch = Regex.Match(line, @".*");

    if (theMatch.Success)
    {
        // Stores the matched value in string output.
        string output = theMatch.Value;

        // Replaces tabs and extra space with only 1 space delimiter
        output = Regex.Replace(output, @"\s+", " ");

        // Splits the specified regex into two different regexs.
        var componentItem = output.Split(' ');

        double d1 = Convert.ToDouble(componentItem[2]);
        double d2 = Convert.ToDouble(componentItem[3]);
        double round1 = Math.Round(d1, 2, MidpointRounding.AwayFromZero);
        double round2 = Math.Round(d2, 2, MidpointRounding.AwayFromZero);

        componentItem[2] = Regex.Replace(componentItem[2], @".*", round1.ToString());
        componentItem[3] = Regex.Replace(componentItem[3], @".*", round2.ToString());

        // Sets the RichTextBox to the string output.
        newPl2ItemsRichTextBox.AppendText(componentItem[0] + "   " + componentItem[1] + "   " + componentItem[2] +
            "   " + componentItem[3] + "   " + componentItem[4] + "   " + componentItem[5] + "\n");
    }
}

有人知道为什么会这样吗?

【问题讨论】:

  • 当您删除 regex.replace(...) 并仅使用 compontentItem[2] = round1.ToString(); 时会发生什么?
  • 这样做的全部目的是简单地将两个数字四舍五入吗?
  • @c0deNinja: 是的,但是我已经提前解决了一些困难

标签: c# regex replace richtextbox


【解决方案1】:

不要做所有这些,只需进行拆分,因为您知道索引 2 和 3 包含您的数字...只需执行以下操作:

newPl2ItemsRichTextBox.AppendText(componentItem[0] + " " + componentItem[1] + " " + Math.Round(Convert.ToDouble(componentItem[2]), 2) + " " + Math.Round(Convert.ToDouble (componentItem[3]), 2) + " " + componentItem[4] + " " + componentItem[5] + "\n");

避免所有其他步骤,只需拆分和打印。

【讨论】:

    【解决方案2】:

    回答您的问题: 在 227.905 中间有一个点可能让替换功能在 227 和 905 上起作用。这就是为什么要插入两次四舍五入的数字。

    【讨论】:

    • 几乎,是的,有两个匹配项,但不是你提到的那些。请参阅下面的答案。
    【解决方案3】:

    您的表达式“.*”匹配两个匹配项: 尝试以下代码重现:

    static void Main(string[] args)
        {
            Regex regex = new Regex(@".*");
            MatchCollection matches = regex.Matches("  227.905  ");
            foreach (var match in matches)
            {
                Console.WriteLine("[{0}]", match);
            }
            Console.ReadKey();
        }
    

    匹配项是: “ 227.905 ”和 ""

    【讨论】:

      猜你喜欢
      • 2016-04-21
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 2017-06-10
      相关资源
      最近更新 更多