C#-查找/编辑/替换字符串 [英] C# - Find/Edit/Replace String

查看:150
本文介绍了C#-查找/编辑/替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文件中检索数据以进行更改并替换旧数据。

I am trying to retrieve data from a file to change it and replace the old data.

我有一个看起来像这样的文件:

I have a file that looks something like this:

TEXT  NEXT_TEXT  10.505   -174.994 0  
TEXT  NEXT_TEXT  100.005  174.994  90  
TEXT  NEXT_TEXT  -10.000  -5.555   180  
TEXT  NEXT_TEXT  -500.987 5.123    270  
TEXT  NEXT_TEXT  987.123  1.000    180  
TEXT  NEXT_TEXT  234.567  200.999  90 

我试图获取第三和第四列,以根据用户输入到两个 TextBoxes 中的内容来更改数据。让我们标记第3列 X 第4列 Y 。这样,还有两个标记为 xTextBox yTextBox 的文本框。这些文本框允许用户输入数字,并且他们在(正,负和/或十进制最多3个小数位)中输入的数字对于每个文本框(X和Y)将为 ADDED 添加到 X列值和 Y列值。

I am trying to grab the 3rd and 4th columns to change the data based on what a user inputs into two TextBoxes. Let's label the 3rd column "X" and the 4th column "Y". With this, there are also two TextBoxes labeled "xTextBox" and "yTextBox". These textboxes allow the user to enter in numbers and the number they enter in (postive, negative, and/or a decimal up to 3 decimal places) for each textbox (X and Y) will be ADDED to the "X" column values and the "Y" column values.

现在,我正在使用此 CODE (如下所示)以剥离 X和 Y列,并将其值显示在标记为 xRichTextBox 的不同 RichTextBoxes 中>和 yRichTextBox

Right now, I am using this CODE (below) to strip the "X" and "Y" columns and display their values into different RichTextBoxes labeled xRichTextBox and yRichTextBox.

因此, xRichTextBox 看起来像这样:

So, the xRichTextBox looks like this:

10.505
100.005
-10.000
-500.987
987.123
234.567

yRichTextBox 看起来像这样:

and the yRichTextBox looks like this:

-174.994
174.994
-5.555
5-123
1.000
200.999






代码:


CODE:

    private void calculateXAndYPlacementTwo()
    {
        // Reads the lines in the file to format.
        var fileReader = File.OpenText(filePath);

        // Creates a list for the lines to be stored in.
        var fileList = new List<string>();

        // Adds each line in the file to the list.
        while (true)
        {
            var line = fileReader.ReadLine();
            if (line == null)
                break;
            fileList.Add(line);
        }

        // Creates new lists to hold certain matches for each list.
        var xyResult = new List<string>();
        var xResult = new List<string>();
        var yResult = new List<string>();

        // Iterate over each line in the file and extract the x and y values
        fileList.ForEach(line =>
        {
            Match xyMatch = Regex.Match(line, @"(?<x>-?\d+\.\d+)\s+(?<y>-?\d+\.\d+)");
            if (xyMatch.Success)
            {
                // grab the x and y values from the regular expression match
                String xValue = xyMatch.Groups["x"].Value;
                String yValue = xyMatch.Groups["y"].Value;

                // add these two values, separated by a space, to the "xyResult" list.
                xyResult.Add(String.Join(" ", new[]{ xValue, yValue }));

                // Adds the values into the xResult and yResult lists.
                xResult.Add(xValue);
                yResult.Add(yValue);

                // Place the 'X' and 'Y' values into the proper RTB.
                xRichTextBox.AppendText(xValue + "\n");
                yRichTextBox.AppendText(yValue + "\n");
            }
        });
    }






SO ,例如,如果用户为 xTextBox -20输入 10.005 对于 yTextBox 已更新 xRichTextBox 如下所示:


SO for example if the user enters in "10.005" for the xTextBox and "-20" for the yTextBox the updated xRichTextBox would look like this:

20.510
110.010
0.005
-490.982
997.128
224.572

更新 yRichTextBox 看起来像这样:

-194.994
154.994
-25.555
-14.877
-19.000
180.999






发生这种情况后,我尝试用更新后的值替换原始文件的第3列和第4列


  • 我当前正在文件中获取值( X和 Y),并将它们输出以分隔 RichTextBoxes ,但是我不知道如何将这些值添加到值从每个 TextBox 输入...我该怎么做?

  • RichTextBox 值是使用'TextBox'值计算的,我该如何获取这些新值并将其添加到原始文件中?

  • I am currently getting the values ("X" and "Y") in the file and outputting them to seperate RichTextBoxes, but I do not know how to add these values to the values entered in from each TextBox... How can I do this?
  • After the RichTextBox values are calculated with the 'TextBox` values, how can I take these new values and add them to the original file?

**这些值并不总是相同,否则我会对其进行硬编码。

**These values are not always the same or else I would hardcode them in.

因此,新文件如下所示:

So, the new file would look like this:

TEXT  NEXT_TEXT  20.510   -194.994 0  
TEXT  NEXT_TEXT  110.010  154.994  90  
TEXT  NEXT_TEXT  0.005    -25.555  180  
TEXT  NEXT_TEXT  -490.982 -14.877  270  
TEXT  NEXT_TEXT  997.128  -19.000  180  
TEXT  NEXT_TEXT  224.572  180.999  90 


推荐答案

您知道如何读取文件,但听起来好像只是在将它们摆正进入文本框。我将创建一个对象来表示一行(带有x,y和最后一列的内容)。当您阅读它们时,请创建一个对象列表。要显示它们,请遍历并选择每个x(如果使用的是4.0,则可以使用linq轻松完成),然后分别选择每个y。要更新,循环浏览并向每个对象添加适当的编号。然后保存,只需再次写出每个对象即可。

You know how to read the files, but it sounds as though you're just putting them straight into the textboxes. I would create an object to represent a line (with an x, y, and whatever the last column is). When you read them in, create a list of the objects. To display them, loop through and select each x (can easily be done with linq if you're using 4.0) and each y for the separate ones. To update, loop through and add the appropriate number to each object. Then to save, just write out each object again.

编辑:仔细查看代码后,您将保留一个X列表和一个X列表。 Ys。创建我提到的对象,然后仅保留这些对象的列表。

After looking more carefully at your code, you're keeping a list of the Xs, and a list of the Ys. Create the object I mentioned and only keep a list of those.

这篇关于C#-查找/编辑/替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆