简体   繁体   English

C# - 查找/编辑/替换字符串

[英]C# - Find/Edit/Replace String

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 

I am trying to grab the 3rd and 4th columns to change the data based on what a user inputs into two TextBoxes .我试图根据用户输入到两个TextBoxes来获取第3 列和第 4 列以更改数据。 Let's label the 3rd column "X" and the 4th column "Y" .让我们标记第三列“X”第四列“Y” With this, there are also two TextBoxes labeled "xTextBox" and "yTextBox" .有了这个,还有两个TextBoxes 标记为"xTextBox""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.这些文本框允许用户输入数字,并且他们为每个文本框(X 和 Y)输入的数字(正数、负数和/或最多 3 个小数位的小数)将被添加到“X”列值和“Y”列值。

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 .现在,我正在使用此CODE (如下)去除“X”和“Y”列并将它们的值显示到不同的RichTextBoxes标记为xRichTextBoxyRichTextBox

So, the xRichTextBox looks like this:所以, xRichTextBox看起来像这样:

10.505
100.005
-10.000
-500.987
987.123
234.567

and the yRichTextBox looks like this: yRichTextBox看起来像这样:

-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 for example if the user enters in "10.005" for the xTextBox and "-20" for the yTextBox the updated xRichTextBox would look like this:因此,例如,如果用户在“10.005”进入了xTextBox“-20”yTextBox更新xRichTextBox是这样的:

20.510
110.010
0.005
-490.982
997.128
224.572

and the updated yRichTextBox would look like this:更新后的yRichTextBox将如下所示:

-194.994
154.994
-25.555
-14.877
-19.000
180.999

After this takes place, I am trying to replace the 3rd and 4th columns of the original file with the updated values.发生这种情况后,我试图用更新后的值替换原始文件的第 3 列和第 4 列

  • 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?我目前正在获取文件中的值(“X”和“Y”)并将它们输出到单独的RichTextBoxes ,但我不知道如何将这些值添加到从每个TextBox输入的值中......我该怎么做这个?
  • After the RichTextBox values are calculated with the 'TextBox` values, how can I take these new values and add them to the original file?在使用“TextBox”值计算RichTextBox值后,如何获取这些新值并将它们添加到原始文件中?

**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 

I suggest using a structured text file parser to parse and rewrite the file.我建议使用结构化文本文件解析器来解析和重写文件。

There is one in the Microsoft.VisualBasic.FileIO namespace, the TextFieldParser . Microsoft.VisualBasic.FileIO命名空间中有一个TextFieldParser This is a normal .NET assembly, so you can reference it in your code and use in from C#.这是一个普通的 .NET 程序集,因此您可以在代码中引用它并从 C# 中使用它。

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).我会创建一个对象来表示一条线(带有 x、y 以及最后一列的任何内容)。 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.要显示它们,请循环并选择每个 x(如果您使用的是 4.0,可以使用 linq 轻松完成)和每个 y 分别选择。 To update, loop through and add the appropriate number to each object.要更新,请循环并为每个对象添加适当的数字。 Then to save, just write out each object again.然后要保存,只需再次写出每个对象。

Edit: After looking more carefully at your code, you're keeping a list of the Xs, and a list of the Ys.编辑:在更仔细地查看您的代码后,您将保留一个 X 列表和一个 Y 列表。 Create the object I mentioned and only keep a list of those.创建我提到的对象并只保留这些对象的列表。

before u save your date be sure to create string of data to be saved and than using textwriter or streamwriter save all content to the file again.在您保存日期之前,请务必创建要保存的数据字符串,然后再使用 textwriter 或 streamwriter 将所有内容保存到文件中。 I do not think u will be able to change only one portion of file and save it我认为你不能只更改文件的一部分并保存它

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

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