简体   繁体   English

替换返回相同值的2吗?

[英]Replace returns 2 of the same value?

I have a question on why this is happening. 我对为什么会这样有疑问。 First, I'll explain what is going on. 首先,我将解释发生了什么。 I am finding a line in a RichTextBox and taking a Split value and replacing it with the same value but with a decimal limit. 我在RichTextBox找到一行,并取一个Split值,并将其替换为相同的值但带有十进制限制。 Here is what my file looks like: 这是我的文件的样子:

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

BUT for some reason I am getting this when I try to replace and output it back out... (the numbers appear twice in both the 3rd and 4th columns) 但是由于某种原因,当我尝试替换并输出回去时,我得到了这个... (数字在第三列和第四列中都出现两次)

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

AND HERE IS MY CODE... WHAT IS THE ERROR TO MAKE IT DO THIS? 这是我的代码...这样做有什么错误?

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");
    }
}

Does anyone know why this is happening? 有人知道为什么会这样吗?

Instead of doing all this, simply do your split, since you know index 2 and 3 contain your numbers... simply do something like: 无需执行所有操作,只需进行拆分即可,因为您知道索引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"); 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”);

Avoid all the other steps, just split and print. 避免其他所有步骤,只需拆分并打印即可。

Your expression ".*" hits two matches: Try out following code to reproduce: 您的表达式“。*”命中两个匹配项:尝试以下代码重现:

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();
    }

Matches are: " 227.905 " and "" 匹配为:“ 227.905”和“”

回答您的问题:在227.905中,中间有一个点可能使replace函数在227和905上起作用。这就是将舍入的数字插入两次的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM